Skip to content

Instantly share code, notes, and snippets.

@emiel
Last active February 20, 2017 08:58
Show Gist options
  • Save emiel/7afbf7fccb20eb831011864139b25922 to your computer and use it in GitHub Desktop.
Save emiel/7afbf7fccb20eb831011864139b25922 to your computer and use it in GitHub Desktop.
Python nested function overhead
import timeit
t1 = timeit.Timer("doit()", """
def doit():
def foo(l):
return [x**2 for x in l]
def bar(l):
return [x**2 for x in l]
l = [x for x in range(1000)]
foo(l)
bar(l)
""")
t2 = timeit.Timer("doit()", """
def foo(l):
return [x**2 for x in l]
def bar(l):
return [x**2 for x in l]
def doit():
l = [x for x in range(1000)]
foo(l)
bar(l)
""")
print(t2.repeat(10, 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment