MCPcopy Create free account
hub / github.com/ipython/ipython / timeit

Method timeit

IPython/core/magics/execution.py:1012–1198  ·  view source on GitHub ↗

Time execution of a Python statement or expression Usage, in line mode: %timeit [-n -r [-t|-c] -q -p -o] statement or in cell mode: %%timeit [-n -r [-t|-c] -q -p -o] setup_code code code... Time execution of a Python

(self, line='', cell=None, local_ns=None)

Source from the content-addressed store, hash-verified

1010 @line_cell_magic
1011 @needs_local_scope
1012 def timeit(self, line='', cell=None, local_ns=None):
1013 """Time execution of a Python statement or expression
1014
1015 Usage, in line mode:
1016 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
1017 or in cell mode:
1018 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
1019 code
1020 code...
1021
1022 Time execution of a Python statement or expression using the timeit
1023 module. This function can be used both as a line and cell magic:
1024
1025 - In line mode you can time a single-line statement (though multiple
1026 ones can be chained with using semicolons).
1027
1028 - In cell mode, the statement in the first line is used as setup code
1029 (executed but not timed) and the body of the cell is timed. The cell
1030 body has access to any variables created in the setup code.
1031
1032 Options:
1033 -n<N>: execute the given statement <N> times in a loop. If <N> is not
1034 provided, <N> is determined so as to get sufficient accuracy.
1035
1036 -r<R>: number of repeats <R>, each consisting of <N> loops, and take the
1037 best result.
1038 Default: 7
1039
1040 -t: use time.time to measure the time, which is the default on Unix.
1041 This function measures wall time.
1042
1043 -c: use time.clock to measure the time, which is the default on
1044 Windows and measures wall time. On Unix, resource.getrusage is used
1045 instead and returns the CPU user time.
1046
1047 -p<P>: use a precision of <P> digits to display the timing result.
1048 Default: 3
1049
1050 -q: Quiet, do not print result.
1051
1052 -o: return a TimeitResult that can be stored in a variable to inspect
1053 the result in more details.
1054
1055 .. versionchanged:: 7.3
1056 User variables are no longer expanded,
1057 the magic line is always left unmodified.
1058
1059 Examples
1060 --------
1061 ::
1062
1063 In [1]: %timeit pass
1064 8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
1065
1066 In [2]: u = None
1067
1068 In [3]: %timeit u is None
1069 29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Callers

nothing calls this directly

Calls 11

timeitMethod · 0.95
clockFunction · 0.90
TimerClass · 0.85
transformFunction · 0.85
TimeitResultClass · 0.85
parse_optionsMethod · 0.80
ast_parseMethod · 0.80
transform_astMethod · 0.80
parseMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected