Created
December 6, 2013 22:40
-
-
Save wrenoud/7833362 to your computer and use it in GitHub Desktop.
I was curious if there was any efficiency difference between using enumerate() and counting for myself. It appears that enumerate() might be slightly faster.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def enum_the_old_way(): | |
i = 0 | |
for j in myrange: | |
i += 1 | |
def enum_the_new_way(): | |
for i,j in enumerate(myrange): | |
pass | |
myrange = range(10000) | |
if __name__ == "__main__": | |
from timeit import timeit | |
t1 = timeit("enum_the_old_way()", \ | |
setup="from __main__ import enum_the_old_way", \ | |
number=10000) | |
t2 = timeit("enum_the_new_way()", \ | |
setup="from __main__ import enum_the_new_way", \ | |
number=10000) | |
print t1, t2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment