Profiling weirdness: Timer.timeit(), fibonacci and memoization
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Aug 3 19:12:54 EDT 2008
On Sun, 03 Aug 2008 09:46:45 -0500, Rob Williscroft wrote:
> Steven D'Aprano wrote in news:00a529cc$0$20316$c3e8da3 at news.astraweb.com
> in comp.lang.python:
>
>>> So the question is: whats going on with timeit.Timer ?
>>
>> As far as I can see, nothing. I think you have misunderstood the
>> results you got.
>
> No, the answer is that is it repeats a million times. It might better
> be called repeat_one_million_times().
But that would be seriously broken if you call it like this:
Timer.repeat_one_million_times(5, 1000)
It doesn't repeat one million times. It repeats five times, of 1000 loops
each.
> Or put another way, myself and the OP misinterpreted what the call
> t1.repeat( number = 1 ) did.
>
> its a confusing API.
There's certainly confusion happening. Perhaps reading the doc strings
might clear up some confusion? help(Timer.repeat) and help(Timer.timeit)
state very clearly that they default to one million iterations.
> For the OP:
>
> The call t1.timeit() is equivalent to t1.repeat( number = 1000000 ).
No it isn't. Try running the code and looking at the results it gives.
t1.repeat(number=10**6) returns a list of three numbers. The function is
called *three* million times in total.
t1.timeit() returns a single number.
In fact, t1.timeit() is equivalent to
t1.repeat(repeat=1, number=1000000), (apart from it being in a list).
That can be written more simply as: t1.repeat(1)
--
Steven
More information about the Python-list
mailing list