Profiling weirdness: Timer.timeit(), fibonacci and memoization

Rob Williscroft rtw at
Sat Aug 2 23:14:11 CEST 2008

Stefaan Himpe wrote in news:2m3lk.118118$9I1.20910 at newsfe16.ams2 in 

> Nothing weird about this ...
> The difference will become larger as your input value becomes larger.
> You can easily understand why if you try to calculate fib(10) by hand,
> i.e. work through the algorithm with pencil and paper,
> then compare the work you have to do to the memoized version which just 
> takes fib(9) and fib(8) from memory and adds them together.

I think you missed the point.

The problem is that the un-decorated, loop only version takes 
35 seconds when called by timeit.Timer.  However if you apply
the decorator it takes less that a second.  In *both* cases
the function (fib) only gets called once.

Note, I timed the call fib(100) with time.clock() and got a
value of less than 1 ms, the memozed version takes about 10 
times longer.

So the question is: whats going on with timeit.Timer ?


More information about the Python-list mailing list