why are these exec times so different?

Bengt Richter bokr at oz.net
Tue Dec 16 21:17:54 EST 2003


On Wed, 17 Dec 2003 01:09:06 GMT, "r.e.s." <r.s at XXmindspring.com> wrote:

>Should one expect the following execution times 
>to be so different? ... 
>
>Module_A takes about 4 seconds to run:
>x = (stuff_1)
>(stuff_2)
>
>Module_B takes about 80 seconds to run:
>x = (stuff_1)
>def f(y):
>    (stuff_2)
>f(x)
>
>where (stuff_1) and (stuff_2) are respectively 
>the same in both modules.  I understand that 
>they should differ in execution time, but by a
>factor of 20?!
>
>(I'll supply the actual code if requested, but
>I didn't want to clutter things up needlessly.
>I'm using PythonWin, win32all build 163.)
Timing is tricky, because of all the extra time that can sneak in.
So the thing is to make the circumstances for both timings as near
equal as possible. Best if you can run them separately in separate executions
of python, and do a short loop and get the best time of several. Don't do too many
if you have a CPU that gets hot and slows its clock to avoid burning. By the same
token, don't do a second test before it's cooled off from the first.
Looping should get rid of cache-miss time loss, and hopefully also garbage collection
affected loops will be thown out. Usually looping and taking the best will be ok to do
in the same program for several tests, but sometimes not.

Make sure you are comparing apples and apples too. I.e., if stuff_2 involves copying a
huge list or using range instead of xrange etc., it's not a fair comparison. Generally
stuff done with local variables inside a function ought to be faster than the same
accessing globals.

Not to mention all the background stuff that could be going on. Don't be dragging windows
around or be playing solitaire or playing music or downloading while testing. Maybe some
spy-ware kicked in and called home? ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list