[Tutor] timeit at the command line

Dick Moores rdm at rcblue.com
Mon Oct 2 06:28:24 CEST 2006


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.

<http://www.python.org/doc/2.3.5/lib/node399.html> says:
"A multi-line statement may be given by specifying each line as a 
separate statement argument; indented lines are possible by enclosing 
an argument in quotes and using leading spaces. Multiple -s options 
are treated similarly."

Thanks,

Dick Moores




More information about the Tutor mailing list