Dick Moores wrote: > At 03:05 AM 10/3/2006, Kent Johnson wrote: >> timeit runs the setup code once, then runs the timed code many times >> with the timer running. If "x=0" is outside the loop, then the while >> loop only runs once, because x == 100 after the first time through the >> loop. So your first version is effectively timing this: >> >> setup: >> x=100 >> >> timed code: >> while x<100: >> x+=1 >> >> which is of course a lot faster than actually running the loopp 100 times. > > Thanks, Kent. I was beginning to understand this, and now you've > nailed it down for me.
You might want to look at the source, timeit.py. There is a code template (called 'template') near the beginning. Your setup and timed code are inserted into the template, then it is compiled and run. One of the dangers of timeit is that you may time something different than what you think you are timing, as you did. It's an easy mistake to make and hard to protect against. Kent _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
