Skip to content

Instantly share code, notes, and snippets.

@IlianIliev
Created January 28, 2013 10:00
Show Gist options
  • Save IlianIliev/4654335 to your computer and use it in GitHub Desktop.
Save IlianIliev/4654335 to your computer and use it in GitHub Desktop.
Measuring execution of multiple functions with timeit
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