10.10.1 Command Line Interface

When called as a program from the command line, the following form is used:

python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]

where the following options are understood:

-n N/--number=N
how many times to execute 'statement'
-r N/--repeat=N
how many times to repeat the timer (default 3)
-s S/--setup=S
statement to be executed once initially (default 'pass')
-t/--time
use time.time() (default on all platforms but Windows)
-c/--clock
use time.clock() (default on Windows)
-v/--verbose
print raw timing results; repeat for more digits precision
-h/--help
print a short usage message and exit

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.

If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds.

The default timer function is platform dependent. On Windows, time.clock() has microsecond granularity but time.time()'s granularity is 1/60th of a second; on Unix, time.clock() has 1/100th of a second granularity and time.time() is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing. The best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. The -r option is good for this; the default of 3 repetitions is probably enough in most cases. On Unix, you can use time.clock() to measure CPU time.

Note: There is a certain baseline overhead associated with executing a pass statement. The code here doesn't try to hide it, but you should be aware of it. The baseline overhead can be measured by invoking the program without arguments.

The baseline overhead differs between Python versions! Also, to fairly compare older Python versions to Python 2.3, you may want to use Python's -O option for the older versions to avoid timing SET_LINENO instructions.

See About this document... for information on suggesting changes.