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
Related videos on Youtube
Author by
ichigo
Updated on May 18, 2022Comments
-
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 over 13 yearsThey are not really interchangeable. What are you trying to do? Can you paste a code sample?
-
Fred Nurk over 13 yearsWhy not test it (after determining that it matters)?
-
Benjamin over 13 yearsWow, 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 over 13 years@Adam: They are if you want to have an index variable handy while you iterate.
-
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 over 13 yearsThe question was actually asking about
xrange
, notrange
-
cemper93 over 13 yearsIn 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 over 13 yearsOkay, 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 over 13 yearsAnd 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 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 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 about 9 yearsaccording to this page pythonfasterway.uni.me enumerate is faster than xrange. It is true. I tried it too.
-
iperov almost 4 yearsin my tests enumerate is faster, python 3.6