Problem with timeit

Steve D'Aprano steve+python at pearwood.info
Fri Dec 15 08:23:52 EST 2017


On Fri, 15 Dec 2017 10:47 pm, Thomas Jollans wrote:

> On 2017-12-15 11:36, ast wrote:
>> Hi
>> 
>> Time measurment with module timeit seems to work with some statements
>> but not with some other statements on my computer.
>> 
>> Python version 3.6.3
>> 
>> from timeit import Timer
>> 
>>>>> Timer("'-'.join([str(i) for i in range(10)])").timeit(10000)
>> 0.179271876732912
>>>>> Timer("'-'.join([str(i) for i in range(10)])").timeit(100000)
>> 1.7445643231192776
>> 
>> It's OK, with 10 more loops I get 10 more execution time.
>> 
>> But with exponentiation, it's a mess
>> 
>>>>> Timer("x=123456**123456").timeit(1)
>> 6.076191311876755e-06
>>>>> Timer("x=123456**123456").timeit(10)
>> 3.841270313387213e-06
>> 
>> All wrong, the calculation of 123456**123456 is much longer
>> than 6 microseconds, it takes few seconds, and with 10 loops timeit
>> provided a shorter time ...
> 
> No, this is right. The calculation takes practically no time; on my
> system, it takes some 10 ns. The uncertainty of the timeit result is at
> least a few hundred nanoseconds.

You've misdiagnosed Ast's problem, and fallen for the same Gotcha he has.
You're not measuring what you think you measured:

>>>> Timer("x=123456**123456").timeit(10**6)
> 0.00969144597183913

Calculating BigInt exponentiation is fast, but its not that fast once you get
to hundreds of thousands of digits.

That calculates the 600,000+ digit number 123456**123456 once, at compile
time, then simply assigns that huge number to x a million times. So you would
likely get almost the same result by running:

Timer("x=17").timeit(10**6)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list