[Tutor] timeit at the command line

Kent Johnson kent37 at tds.net
Mon Oct 2 12:08:24 CEST 2006


Dick Moores wrote:
> C:\>python -m timeit  -s"x=0" "while x<100:" "  x+=1"
> 10000000 loops, best of 3: 0.123 usec per loop
> 
> C:\>python -m timeit -s"for x in range(100):" "    x+=1"
> Traceback (most recent call last):
>    File "E:\Python25\lib\runpy.py", line 95, in run_module
>      filename, loader, alter_sys)
>    File "E:\Python25\lib\runpy.py", line 52, in _run_module_code
>      mod_name, mod_fname, mod_loader)
>    File "E:\Python25\lib\runpy.py", line 32, in _run_code
>      exec code in run_globals
>    File "E:\Python25\lib\timeit.py", line 285, in <module>
>      sys.exit(main())
>    File "E:\Python25\lib\timeit.py", line 249, in main
>      t = Timer(stmt, setup, timer)
>    File "E:\Python25\lib\timeit.py", line 116, in __init__
>      code = compile(src, dummy_src_name, "exec")
>    File "<timeit-src>", line 4
>      _t0 = _timer()
>        ^
> IndentationError: expected an indented block
> 
> 
> Is there a better way to indicate the indentation of "  x+=1"  (2 
> spaces in this case) ? For me, this usually works in a while loop, 
> but so far, never in a for loop.

Strange, your example works for me in Python 2.4 and 2.5:
D:\>python -m timeit  -s"x=0" "while x<100:" "  x+=1"
10000000 loops, best of 3: 0.102 usec per loop

Are you using 2.5 final or one of the earlier releases?

Kent



More information about the Tutor mailing list