Skip to content

Instantly share code, notes, and snippets.

@wrenoud
Created December 6, 2013 22:40
Show Gist options
  • Save wrenoud/7833362 to your computer and use it in GitHub Desktop.
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.
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