1 | initial version |
You can check the documentation for the %timeit
magic by running %timeit?
and it will normally be displayed in a pager that you can scroll with the arrow keys (if in the console) or in the help window (in a notebook). To print the full docs to put here for posterity though, I did:
In [4]: import IPython.core.magics.execution
In [5]: print(IPython.core.magics.execution.timeit.__doc__)
Tool for measuring execution time of small code snippets.
This module avoids a number of common traps for measuring execution
times. See also Tim Peters' introduction to the Algorithms chapter in
the Python Cookbook, published by O'Reilly.
Library usage: see the Timer class.
Command line usage:
python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement]
Options:
-n/--number N: how many times to execute 'statement' (default: see below)
-r/--repeat N: how many times to repeat the timer (default 3)
-s/--setup S: statement to be executed once initially (default 'pass').
Execution time of this setup statement is NOT timed.
-p/--process: use time.process_time() (default is time.perf_counter())
-t/--time: use time.time() (deprecated)
-c/--clock: use time.clock() (deprecated)
-v/--verbose: print raw timing results; repeat for more digits precision
-u/--unit: set the output time unit (usec, msec, or sec)
-h/--help: print this usage message and exit
--: separate options from statement, use when statement starts with -
statement: statement to be timed (default 'pass')
A multi-line statement may be given by specifying each line as a
separate 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.
Note: there is a certain baseline overhead associated with executing a
pass statement. It differs between versions. 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.
Classes:
Timer
Functions:
timeit(string, string) -> float
repeat(string, string) -> list
default_timer() -> float
Basically, in the default behavior, timeit will do multiple trials (typically three) where it loops over the function/code being timed some number of times, where the default number of loops is adjusted automatically based on the length of time taken the first few times through the loop. The result of each trial is an average time based on the number of loops. Multiple trials are performed just because depending on various external factors, each trial could give a different result, and it will give you the best result.
So in the case of "625 loops, best of 3: 38.1 ns per loop what is the meaning of 625 loops, best of 3", three trials were performed, in each trial the code was looped over 625 times, and "per loop" here means that the code took on average 38.1 ns for one pass through the loop, which is admittedly not entirely clear.
So in other words, it's saying out of 3 trials, it seems the average time for the code being timed is at best 38.1 ns
. Though as you can see above there are many options for controlling the number of loops, the number of trials, etc.
2 | No.2 Revision |
You can check the documentation for the %timeit
magic by running %timeit?
and it will normally be displayed in a pager that you can scroll with the arrow keys (if in the console) or in the help window (in a notebook). To print the full docs to put here for posterity though, I did:
In [4]: import IPython.core.magics.execution
In [5]: print(IPython.core.magics.execution.timeit.__doc__)
Tool for measuring print(IPython.core.magics.execution.ExecutionMagics.timeit.__doc__)
Time execution of a Python statement or expression
Usage, in line mode:
%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
code
code...
Time execution of a Python statement or expression using the timeit
module. This function can be used both as a line and cell magic:
- In line mode you can time of small code snippets.
a single-line statement (though multiple
ones can be chained with using semicolons).
- In cell mode, the statement in the first line is used as setup code
(executed but not timed) and the body of the cell is timed. The cell
body has access to any variables created in the setup code.
Options:
-n<N>: execute the given statement <N> times in a loop. If this value
is not given, a fitting value is chosen.
-r<R>: repeat the loop iteration <R> times and take the best result.
Default: 3
-t: use time.time to measure the time, which is the default on Unix.
This module avoids a number of common traps for measuring execution
times. See also Tim Peters' introduction function measures wall time.
-c: use time.clock to the Algorithms chapter in
the Python Cookbook, published measure the time, which is the default on
Windows and measures wall time. On Unix, resource.getrusage is used
instead and returns the CPU user time.
-p<P>: use a precision of <P> digits to display the timing result.
Default: 3
-q: Quiet, do not print result.
-o: return a TimeitResult that can be stored in a variable to inspect
the result in more details.
Examples
--------
::
In [1]: %timeit pass
8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
In [2]: u = None
In [3]: %timeit u is None
29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [4]: %timeit -r 4 u == None
In [5]: import time
In [6]: %timeit -n1 time.sleep(2)
The times reported by O'Reilly.
Library usage: see the Timer class.
Command line usage:
python %timeit will be slightly higher than those
reported by the timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement]
Options:
-n/--number N: how many times script when variables are accessed. This is
due to execute 'statement' (default: see below)
-r/--repeat N: how many times to repeat the timer (default 3)
-s/--setup S: the fact that %timeit executes the statement in the namespace
of the shell, compared with timeit.py, which uses a single setup
statement to be executed once initially (default 'pass').
Execution time of this setup statement is NOT timed.
-p/--process: use time.process_time() (default is time.perf_counter())
-t/--time: use time.time() (deprecated)
-c/--clock: use time.clock() (deprecated)
-v/--verbose: print raw timing results; repeat for more digits precision
-u/--unit: set the output time unit (usec, msec, import function or sec)
-h/--help: print this usage message and exit
--: separate options create variables. Generally, the bias
does not matter as long as results from statement, use when statement starts with -
statement: statement to be timed (default 'pass')
A multi-line statement may be given by specifying each line as a
separate argument; indented lines timeit.py 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.
Note: there is a certain baseline overhead associated with executing a
pass statement. It differs between versions. 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.
Classes:
Timer
Functions:
timeit(string, string) -> float
repeat(string, string) -> list
default_timer() -> float
mixed with
those from %timeit.
Basically, in the default behavior, timeit will do multiple trials (typically three) where it loops over the function/code being timed some number of times, where the default number of loops is adjusted automatically based on the length of time taken the first few times through the loop. The result of each trial is an average time based on the number of loops. Multiple trials are performed just because depending on various external factors, each trial could give a different result, and it will give you the best result.
So in the case of "625 loops, best of 3: 38.1 ns per loop what is the meaning of 625 loops, best of 3", three trials were performed, in each trial the code was looped over 625 times, and "per loop" here means that the code took on average 38.1 ns for one pass through the loop, which is admittedly not entirely clear.
So in other words, it's saying out of 3 trials, it seems the average time for the code being timed is at best 38.1 ns
. Though as you can see above there are many options for controlling the number of loops, the number of trials, etc.