here is the code to calculate fibonacci
import timeit
counter=0
def fibhelper(n):
global counter
counter+=1
if n==0 :
return 0
elif n==1:
return 1
else:
return fibhelper(n-1)+fibhelper(n-2)
print fibhelper(20)
print "Total function calls-- ",counter
t1=timeit.Timer('fibhelper(20)',"from __main__ import fibhelper")
y=t1.timeit()
print "normal method in secs: ",y
output is:
6765
Total function calls-- 21891
which comes out immediately, but it is still calculating y
. why is this? when the function
is evaluated quickly, why does timeit
of that function
takes longer?