Skip to content

Instantly share code, notes, and snippets.

@sapamja
Created April 6, 2014 08:40
Show Gist options
  • Save sapamja/10003182 to your computer and use it in GitHub Desktop.
Save sapamja/10003182 to your computer and use it in GitHub Desktop.
comparing function return assigning variable vs not.
#!/usr/bin/python
from memory_profiler import profile
@profile
def func1(a):
if a > 0:
num = 1
else:
num = -1
return num
#@profile
def func2(a):
if a > 0:
return 1
else:
return -1
func = [ func1, func2 ]
import cProfile
from timeit import Timer
import dis
for fun in func:
print fun(100)
t = Timer(lambda: fun(-1))
print fun.__name__, cProfile.run('t.timeit(number=1000)')
print fun.__name__
print dis.dis(fun)
print '-------'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment