@@ -98,47 +98,55 @@ The module defines three convenience functions and a public class:
98
98
99
99
.. class :: Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)
100
100
101
- Class for timing execution speed of small code snippets.
102
-
103
- The constructor takes a statement to be timed, an additional statement used
104
- for setup, and a timer function. Both statements default to ``'pass' ``;
105
- the timer function is platform-dependent (see the module doc string).
106
- *stmt * and *setup * may also contain multiple statements separated by ``; ``
107
- or newlines, as long as they don't contain multi-line string literals. The
108
- statement will by default be executed within timeit's namespace; this behavior
109
- can be controlled by passing a namespace to *globals *.
110
-
111
- To measure the execution time of the first statement, use the :meth: `.timeit `
112
- method. The :meth: `.repeat ` and :meth: `.autorange ` methods are convenience
113
- methods to call :meth: `.timeit ` multiple times.
114
-
115
- The execution time of *setup * is excluded from the overall timed execution run.
116
-
117
- The *stmt * and *setup * parameters can also take objects that are callable
118
- without arguments. This will embed calls to them in a timer function that
119
- will then be executed by :meth: `.timeit `. Note that the timing overhead is a
120
- little larger in this case because of the extra function calls.
101
+ Class for timing execution speed of small code snippets. A Timeit instance
102
+ will contain a function (see :meth: `Timer.timeit `) that executes a snippet of
103
+ *setup * code once and then times some number of executions of *stmt * code. The
104
+ code snippets, given as arguments *setup * and *stmt * when creating the
105
+ instance, may be either strings or callable objects.
106
+
107
+ If *setup * or *stmt * is provided as a string, it may contain a python
108
+ expression, statement, or multiple statements separated by ";" or newlines.
109
+ Whitespace adhering to the usual Python indentation rules must follow any
110
+ newlines.
111
+
112
+ If *setup * or *stmt * is a callable object, (often a function), the object is
113
+ called with no arguments. Note that the timing overhead is a little larger in
114
+ this case because of the extra function calls required.
115
+
116
+ The *setup * and *stmt * parameters default to 'pass'. The *timer * parameter
117
+ defaults to a platform-dependent timer function (see the module doc string).
118
+
119
+ When *setup * and *stmt * are run, they are run in a different namespace than
120
+ that of the code that calls :meth: `Timer.timeit() `. To give *stmt * (whether it
121
+ is a callable name or code string) access to objects defined in the code that
122
+ calls timeit, *setup * can import any needed objects. For example, if your code
123
+ defines function ``testfunc() ``, *setup * can contain, ``from __main__ import
124
+ testfunc ``, and code in *stmt * can then call ``testfunc ``.
125
+
126
+ To measure the execution time of *stmt *, use the :meth: `Timer.timeit() ` method.
127
+ The :meth: `Timer.repeat() ` method is a convenience to call :meth: `Timer.timeit() `
128
+ multiple times and return a list of results.
121
129
122
130
.. versionchanged :: 3.5
123
131
The optional *globals * parameter was added.
124
132
125
133
.. method :: Timer.timeit(number=1000000)
126
134
127
- Time *number * executions of the main statement. This executes the setup
128
- statement once, and then returns the time it takes to execute the main
129
- statement a number of times. The default timer returns seconds as a float.
135
+ Time *number * executions of the main snippet. This executes the setup
136
+ snippet once, and then returns the time it takes to execute the main
137
+ snippet a number of times. The default timer returns seconds as a float.
130
138
The argument is the number of times through the loop, defaulting to one
131
- million. The main statement , the setup statement and the timer function
139
+ million. The main snippet , the setup snippet and the timer function
132
140
to be used are passed to the constructor.
133
141
134
142
.. note ::
135
143
136
144
By default, :meth: `.timeit ` temporarily turns off :term: `garbage
137
- collection ` during the timing. The advantage of this approach is that
138
- it makes independent timings more comparable. The disadvantage is
145
+ collection ` during the timing. The advantage of this approach is that
146
+ it makes independent timings more comparable. The disadvantage is
139
147
that GC may be an important component of the performance of the
140
- function being measured. If so, GC can be re-enabled as the first
141
- statement in the *setup * string. For example::
148
+ function being measured. If so, GC can be re-enabled as the first
149
+ snippet in the *setup * string. For example::
142
150
143
151
timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
144
152
0 commit comments