import numpy as np class Store(object): pass def function0(): y = 0 # x = np.random.randn(20,20) # y = np.linalg.inv(x) # y = np.corrcoef(x) return y def function1(): result = Store() result.a = np.arange(5) result.b = function0() return result def function2(): return np.arange(5), function0() def function3(): result = function1() return result.a, result.b def function4(full_output=True): if full_output: result = Store() result.a = np.arange(5) result.b = function0() return result else: return np.arange(5), function0() if __name__=='__main__': from timeit import Timer n = 10000 t = Timer("r = function1()", "from __main__ import np,Store,function1") t1 = t.timeit(n) print 'function1', t1 t = Timer("r = function2()", "from __main__ import np,Store,function2") t2 = t.timeit(n) print 'function2', t2 t = Timer("r = function3()", "from __main__ import np,Store,function3") t3 = t.timeit(n) print 'function3', t3, t3/t2 t = Timer("r = function1()", "from __main__ import np,Store,function1") t1 = t.timeit(n) #repeated print 'function1', t1, t1/t2 t = Timer("r = function4()", "from __main__ import np,Store,function4") t4a = t.timeit(n) print 'function4a', t4a, t4a/t2 t = Timer("r = function4(full_output=0)", "from __main__ import np,Store,function4") t4b = t.timeit(n) print 'function4b', t4b, t4b/t2