What is faster for loop using enumerate or for loop using xrange in Python?

18,418

Solution 1

Enumerate is slightly faster. Tested in Python 3:

>>>import pygame
>>>pygame.init()
>>>clock = pygame.time.Clock()
>>>a = list(range(100000))
>>>def do_with_range():
...    clock.tick()
...    k = 0
...    for i in range(len(a)):
...        k += a[i]
...    print(clock.tick())
>>>def do_with_enumerate():
...    clock.tick()
...    k = 0
...    for i, j in enumerate(a):
...        k += j
...    print(clock.tick())
>>>do_with_range()
23
>>>do_with_enumerate()
21

If a wouldn't be a list, but a generator, it would be significantly faster to use enumerate (74ms using range, 23ms using enumerate).

Solution 2

You can use the timeit module in the standard library to compare both. The timeit.timeit() function used below takes a statement that it runs 1'000'000 times and returns the total time in seconds. In this test enumerate() is slightly slower.

>>> import timeit
>>> timeit.timeit('for i in xrange(100): a[i]', 'a = list(xrange(100))')
7.2920000553131104
>>> timeit.timeit('for i, o in enumerate(a): o', 'a = list(xrange(100))')
10.359999895095825
>>> timeit.timeit('for i in xrange(100): a[i] + 1', 'a = list(xrange(100))')
10.380000114440918
>>> timeit.timeit('for i, o in enumerate(a): o + 1', 'a = list(xrange(100))')
13.514999866485596
Share:
18,418

Related videos on Youtube

ichigo
Author by

ichigo

Updated on May 18, 2022

Comments

  • ichigo
    ichigo about 2 years

    What is faster, a for loop using enumerate or using xrange?

    EDIT: I have tested, and I just see minimal differences.

    • Adam Matan
      Adam Matan over 13 years
      They are not really interchangeable. What are you trying to do? Can you paste a code sample?
    • Fred Nurk
      Fred Nurk over 13 years
    • Benjamin
      Benjamin over 13 years
      Wow, downvote heaven over here. Let's wait a little for an improved question. Perhaps the poster needs an answer as to why this question is difficult to answer... Let's be a little more welcoming to this new user and help him along on his first post.
    • Alex S
      Alex S over 13 years
      @Adam: They are if you want to have an index variable handy while you iterate.
    • Alex S
      Alex S over 13 years
      @Jochen: High-octane gas will make some cars go faster, and using the right mixture is likely to be much cheaper that buying a different car. As for the question, I think it's perfectly reasonable to ask which is faster. Sometimes little things like this can make a big difference to performance-critical sections of code. In JavaScript, for instance, choosing the right looping construct can make a huge difference.
  • Richard Levasseur
    Richard Levasseur over 13 years
    The question was actually asking about xrange, not range
  • cemper93
    cemper93 over 13 years
    In Python 3 xrange got replaced by range and range is a generator now. This is also why i manually did list(range()). Thus, the results in Python 2 using xrange would be similar.
  • cemper93
    cemper93 over 13 years
    Okay, I tested it in Python 2 as well. For obscure reasons, Python 2 is a hell of a lot faster than Python 3, and the xrange and enumerate versions are of the same speed: 14ms. (Shouldn't one expect later versions of the language to be faster than previous ones? But well...).
  • Nick Bastin
    Nick Bastin over 13 years
    And yet it's still faster to use a local that you increment rather than either of these methods. Enumerate is the Pythonic way, but unpacking tuples is slower than incrementing locals, and so it will probably always be slower (although only marginally so).
  • Admin
    Admin over 13 years
    @Nick: Not to mention that the difference will be ridiculous, if noticeable at all, even a noticeable difference wouldn't justify the extra code and ugliness unless perhaps in an inner loop (which you should propably just write in Cython if speed is that important).
  • Nick Bastin
    Nick Bastin over 13 years
    @delnan: well, this actually comes into play using LLVM, where JIT optimizing += is quite different than JIT optimizing a function result being unpacked into two locals. Not that stackoverflow comments are the place to discuss the finer details of python efficiency.. :-) But suffice it to say that the global resource consumption of using enumerate() on a cluster can actually be a significant disadvantage compared to +=.
  • wookie
    wookie about 9 years
    according to this page pythonfasterway.uni.me enumerate is faster than xrange. It is true. I tried it too.
  • iperov
    iperov almost 4 years
    in my tests enumerate is faster, python 3.6