Created
January 28, 2013 10:00
-
-
Save IlianIliev/4654335 to your computer and use it in GitHub Desktop.
Measuring execution of multiple functions with timeit
This file contains 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
import timeit | |
NUM = 10**10000 | |
def f1(): | |
x = [chr(i**i) for i in xrange(NUM) if i%2==1] | |
return x | |
def f2(): | |
x = [] | |
for i in range(NUM): | |
x.append(chr(i**i)) | |
return x | |
def f3(): | |
x = map(lambda x:chr(x**x), range(NUM)) | |
return x | |
my_locals = locals().copy() | |
for name in my_locals: | |
if name.startswith('f'): | |
func = my_locals[name] | |
setup = 'from __main__ import %s' % name | |
number = 10**6 | |
print '%s: %s' % (name, timeit.timeit(name, setup=setup, number=number)/number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment