Skip to content

Instantly share code, notes, and snippets.

@funnydman
Created July 12, 2019 13:27
Show Gist options
  • Save funnydman/b52d8165ce739653869f99b7bc94790f to your computer and use it in GitHub Desktop.
Save funnydman/b52d8165ce739653869f99b7bc94790f to your computer and use it in GitHub Desktop.
import timeit
setup = '''
import random
random.seed('slartibartfast')
s = [random.random() for i in range(1000)]
timsort = list.sort
'''
print(min(timeit.Timer('a=s[:]; timsort(a)', setup=setup).repeat(7, 1000)))
@funnydman
Copy link
Author

funnydman commented Aug 3, 2019

With IPython:

In [1]: def f(x):
   ...:     return x*x
   ...: 

In [2]: %timeit for x in range(100): f(x)
100000 loops, best of 3: 20.3 us per loop

With plain interpreter:

>>> def f(x):
...     return x * x 
... 
>>> import timeit
>>> timeit.repeat("for x in range(100): f(x)", "from __main__ import f",
                  number=100000)
[2.0640320777893066, 2.0876040458679199, 2.0520210266113281]

https://stackoverflow.com/questions/8220801/how-to-use-timeit-module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment