Skip to content

Instantly share code, notes, and snippets.

@ChaiBapchya
Created April 11, 2021 09:48
Show Gist options
  • Save ChaiBapchya/252f3ba9340006659b830bac06c961ef to your computer and use it in GitHub Desktop.
Save ChaiBapchya/252f3ba9340006659b830bac06c961ef to your computer and use it in GitHub Desktop.
itertools vs for loop speed test
import timeit
print('------------For loop timeit-------------')
testcode='''
a=[]
for i in range(1,1000):
a.append(i)
'''
print(timeit.timeit(stmt=testcode))
print('------------itertool timeit-------------')
import_module = "import itertools as its"
testcode='''
a=[]
for i in its.count(start=0,step=1):
if i<1000:
a.append(i)
else:
break
'''
print(timeit.timeit(stmt=testcode, setup=import_module))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment