-4

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?

brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

3

The default parameters of timeit include: number=1000000.

Quoting the documentation of timeit:

... run its timeit() method with number executions.

Therefore, it is expected to take 1000000 times longer.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194