[Tutor] Astonishing timing result

broek at cc.umanitoba.ca broek at cc.umanitoba.ca
Tue Jun 24 23:06:35 CEST 2008


----- Message from rdm at rcblue.com ---------
     Date: Tue, 24 Jun 2008 13:44:00 -0700
     From: Dick Moores <rdm at rcblue.com>

> At 12:44 PM 6/24/2008, Kent Johnson wrote:
>> On Tue, Jun 24, 2008 at 1:43 PM, Dick Moores <rdm at rcblue.com> wrote:
>>> Output:
>>> t1 is 0.000104, no function
>>> t2 is 5.87e-006, function explicit
>>> t3 is 0.000126, function imported
>>> t1/t2 is 17.8
>>> t1/t3 is 0.827
>>> t3/t2 is 21.5
>>>
>>> Now, I'd heard that code in a function runs faster than the same
>> code not in
>>> a function, but even so I was surprised at the t1/t2 ratio of 17.8.
>>>
>>> The astonishing (to me, anyway) result was the t3/t2 ratio. I had no idea
>>> that importing from mycalc slowed a script down at all, let alone by a
>>> factor of 21!
>>
>> Note that t1 and t3 are pretty close to each other. Perhaps you should
>> be suspicious of t2. What if __name__ != '__main__' ?
>
> With that,
> t1 is 0.000104, no function
> t2 is 0.000117, function explicit
> t3 is 0.000113, function imported
> t1/t2 is 0.885
> t1/t3 is 0.914
> t3/t2 is 0.969
>
> Explain?
>
> Dick


Hey Dick,

I'm not too clear on what it is that you want explained.

It seems to me that the difference between t2 and t3 is 1) is so small  
as to be  most likely due to (effectively) random fluctuations of your  
environment (the demands that other processes were making on your  
system at the time) and 2) so small so as to not be worth worrying  
about (<http://c2.com/cgi/wiki?PrematureOptimization>).

I'd further wager that if you repeat the timing a few times, you'll  
find that on some runs t2 is less than t3.

Best,

Brian vdB


More information about the Tutor mailing list