[Tutor] timeit at the command line

Dick Moores rdm at rcblue.com
Tue Oct 3 05:13:49 CEST 2006


At 01:50 PM 10/2/2006, John Fouhy wrote:
>On 02/10/06, Dick Moores <rdm at rcblue.com> wrote:
>>C:\>python -m timeit -s"for x in range(100):" "    x+=1"
>>Traceback (most recent call last):
>
>The -s option specifies the setup code.  In this case, you don't have
>any setup code.  Try this:
>
>python -m timeit "for x in range(100):" " x += 1"
>
>HTH!
>
>--
>John.

Terrific!
E:\Python25\dev>python -m timeit "for x in range(100):" " x += 1"
100000 loops, best of 3: 14.9 usec per loop

So with
python -m timeit  -s"x=0" "while x<100:" "  x+=1"
10000000 loops, best of 3: 0.123 usec per loop

I DID have setup code, the "x=0".  I now notice that if the "x=0" is 
not stated as the setup code, the time difference is enormous, 
132-to-1 in this case.

python -m timeit  -s"x=0" "while x<100:" "  x+=1"
10000000 loops, best of 3: 0.116 usec per loop

python -m timeit "x=0" "while x<100:" "  x+=1"
100000 loops, best of 3: 15.3 usec per loop

Thanks, John.

Dick






More information about the Tutor mailing list