Skip to content

Instantly share code, notes, and snippets.

@nasturtus
Created April 20, 2017 18:56
Show Gist options
  • Save nasturtus/751b58811ed7f90dd20aeefb99db0b17 to your computer and use it in GitHub Desktop.
Save nasturtus/751b58811ed7f90dd20aeefb99db0b17 to your computer and use it in GitHub Desktop.
# Illustrate timeit to time code snippets.
import timeit
# timeit should be callable and requires two arguments:
# a string and number of iterations.
# A basic timeit
print(timeit.timeit('10 + 2', number=1000000))
# timeit for generator
print(timeit.timeit('''gen = (i for i in range(100))''', number=1000000))
# timeit for list comprehension
print(timeit.timeit('''lst = [i for i in range(100)]''', number=1000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment