Mercurial > cpython-lru_cache-weakref
annotate Lib/timeit.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | d43a8aa9dbef |
| children | dda8a6b8a351 |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
52758
diff
changeset
|
1 #! /usr/bin/env python3 |
|
28533
f213ee6ce55e
add a #! line for unix weenies
Skip Montanaro <skip@pobox.com>
parents:
28531
diff
changeset
|
2 |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
3 """Tool for measuring execution time of small code snippets. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
5 This module avoids a number of common traps for measuring execution |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
6 times. See also Tim Peters' introduction to the Algorithms chapter in |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
7 the Python Cookbook, published by O'Reilly. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
9 Library usage: see the Timer class. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 Command line usage: |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
12 python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement] |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 Options: |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
15 -n/--number N: how many times to execute 'statement' (default: see below) |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
16 -r/--repeat N: how many times to repeat the timer (default 3) |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
17 -s/--setup S: statement to be executed once initially (default 'pass') |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
18 -p/--process: use time.process_time() (default is time.perf_counter()) |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
19 -t/--time: use time.time() (deprecated) |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
20 -c/--clock: use time.clock() (deprecated) |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
21 -v/--verbose: print raw timing results; repeat for more digits precision |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
22 -h/--help: print this usage message and exit |
|
63438
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52758
diff
changeset
|
23 --: separate options from statement, use when statement starts with - |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 statement: statement to be timed (default 'pass') |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
25 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
26 A multi-line statement may be given by specifying each line as a |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
27 separate argument; indented lines are possible by enclosing an |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
28 argument in quotes and using leading spaces. Multiple -s options are |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
29 treated similarly. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
30 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
31 If -n is not given, a suitable number of loops is calculated by trying |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
32 successive powers of 10 until the total time is at least 0.2 seconds. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
33 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
34 The difference in default timer function is because on Windows, |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
35 clock() has microsecond granularity but time()'s granularity is 1/60th |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
36 of a second; on Unix, clock() has 1/100th of a second granularity and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
37 time() is much more precise. On either platform, the default timer |
|
30464
d7f13a49e5ef
Patch #808362: Fix typos.
Martin v. Löwis <martin@v.loewis.de>
parents:
29658
diff
changeset
|
38 functions measure wall clock time, not the CPU time. This means that |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
39 other processes running on the same computer may interfere with the |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
40 timing. The best thing to do when accurate timing is necessary is to |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
41 repeat the timing a few times and use the best time. The -r option is |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
42 good for this; the default of 3 repetitions is probably enough in most |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
43 cases. On Unix, you can use clock() to measure CPU time. |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
44 |
|
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
45 Note: there is a certain baseline overhead associated with executing a |
|
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
46 pass statement. The code here doesn't try to hide it, but you should |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
47 be aware of it. The baseline overhead can be measured by invoking the |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
48 program without arguments. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
50 The baseline overhead differs between Python versions! Also, to |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
51 fairly compare older Python versions to Python 2.3, you may want to |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
52 use python -O for the older versions to avoid timing SET_LINENO |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
53 instructions. |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
54 """ |
|
28200
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
55 |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
56 import gc |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 import sys |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 import time |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
59 try: |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
60 import itertools |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
61 except ImportError: |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
62 # Must be an older Python version (see timeit() below) |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
63 itertools = None |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 __all__ = ["Timer"] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
67 dummy_src_name = "<timeit-src>" |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 default_number = 1000000 |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
69 default_repeat = 3 |
|
76599
bd195749c0a2
Issue #14428: Use the new time.perf_counter() and time.process_time() functions
Victor Stinner <victor.stinner@gmail.com>
parents:
73166
diff
changeset
|
70 default_timer = time.perf_counter |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
72 # Don't change the indentation of the template; the reindent() calls |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
73 # in Timer.__init__() depend on setup being indented 4 spaces and stmt |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
74 # being indented 8 spaces. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 template = """ |
|
28363
bab2b2863a6e
Rename variables _seq to _it and seq to it, to emphasize that this is
Guido van Rossum <guido@python.org>
parents:
28301
diff
changeset
|
76 def inner(_it, _timer): |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
77 {setup} |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
78 _t0 = _timer() |
|
28363
bab2b2863a6e
Rename variables _seq to _it and seq to it, to emphasize that this is
Guido van Rossum <guido@python.org>
parents:
28301
diff
changeset
|
79 for _i in _it: |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
80 {stmt} |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
81 _t1 = _timer() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
82 return _t1 - _t0 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 """ |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 def reindent(src, indent): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
86 """Helper to reindent a multi-line statement.""" |
|
28197
efe249d069ba
Simpler way to write reindent(), suggested by Raymond H.
Guido van Rossum <guido@python.org>
parents:
28184
diff
changeset
|
87 return src.replace("\n", "\n" + " "*indent) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
89 def _template_func(setup, func): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
90 """Create a timer function. Used if the "statement" is a callable.""" |
|
52758
1bf671217abf
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
43577
diff
changeset
|
91 def inner(_it, _timer, _func=func): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
92 setup() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
93 _t0 = _timer() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
94 for _i in _it: |
|
52758
1bf671217abf
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
43577
diff
changeset
|
95 _func() |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
96 _t1 = _timer() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
97 return _t1 - _t0 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
98 return inner |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
99 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 class Timer: |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
101 """Class for timing execution speed of small code snippets. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
102 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
103 The constructor takes a statement to be timed, an additional |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
104 statement used for setup, and a timer function. Both statements |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
105 default to 'pass'; the timer function is platform-dependent (see |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
106 module doc string). |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
107 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
108 To measure the execution time of the first statement, use the |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
109 timeit() method. The repeat() method is a convenience to call |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
110 timeit() multiple times and return a list of results. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
111 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
112 The statements may contain newlines, as long as they don't contain |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
113 multi-line string literals. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
114 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 def __init__(self, stmt="pass", setup="pass", timer=default_timer): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
117 """Constructor. See class doc string.""" |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 self.timer = timer |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 ns = {} |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
120 if isinstance(stmt, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
121 stmt = reindent(stmt, 8) |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
122 if isinstance(setup, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
123 setup = reindent(setup, 4) |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
124 src = template.format(stmt=stmt, setup=setup) |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
125 elif callable(setup): |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
126 src = template.format(stmt=stmt, setup='_setup()') |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
127 ns['_setup'] = setup |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
128 else: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
129 raise ValueError("setup is neither a string nor callable") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
130 self.src = src # Save for traceback display |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
131 code = compile(src, dummy_src_name, "exec") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
132 exec(code, globals(), ns) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
133 self.inner = ns["inner"] |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
134 elif callable(stmt): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
135 self.src = None |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
136 if isinstance(setup, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
137 _setup = setup |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
138 def setup(): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
139 exec(_setup, globals(), ns) |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
140 elif not callable(setup): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
141 raise ValueError("setup is neither a string nor callable") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
142 self.inner = _template_func(setup, stmt) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
143 else: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
144 raise ValueError("stmt is neither a string nor callable") |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
146 def print_exc(self, file=None): |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
147 """Helper to print a traceback from the timed code. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
148 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
149 Typical use: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
150 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
151 t = Timer(...) # outside the try/except |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
152 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
153 t.timeit(...) # or t.repeat(...) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
154 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
155 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
156 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
157 The advantage over the standard traceback is that source lines |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
158 in the compiled template will be displayed. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
159 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
160 The optional file argument directs where the traceback is |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
161 sent; it defaults to sys.stderr. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
162 """ |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
163 import linecache, traceback |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
164 if self.src is not None: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
165 linecache.cache[dummy_src_name] = (len(self.src), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
166 None, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
167 self.src.split("\n"), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
168 dummy_src_name) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
169 # else the source is already stored somewhere else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
170 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
171 traceback.print_exc(file=file) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
172 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 def timeit(self, number=default_number): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
174 """Time 'number' executions of the main statement. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
175 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
176 To be precise, this executes the setup statement once, and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
177 then returns the time it takes to execute the main statement |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
178 a number of times, as a float measured in seconds. The |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
179 argument is the number of times through the loop, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
180 to one million. The main statement, the setup statement and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
181 the timer function to be used are passed to the constructor. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
182 """ |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
183 if itertools: |
|
28363
bab2b2863a6e
Rename variables _seq to _it and seq to it, to emphasize that this is
Guido van Rossum <guido@python.org>
parents:
28301
diff
changeset
|
184 it = itertools.repeat(None, number) |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
185 else: |
|
28363
bab2b2863a6e
Rename variables _seq to _it and seq to it, to emphasize that this is
Guido van Rossum <guido@python.org>
parents:
28301
diff
changeset
|
186 it = [None] * number |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
187 gcold = gc.isenabled() |
|
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
188 gc.disable() |
|
71547
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
189 try: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
190 timing = self.inner(it, self.timer) |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
191 finally: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
192 if gcold: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
193 gc.enable() |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
194 return timing |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 def repeat(self, repeat=default_repeat, number=default_number): |
|
28531
1639f635d983
correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents:
28363
diff
changeset
|
197 """Call timeit() a few times. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
198 |
|
28531
1639f635d983
correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents:
28363
diff
changeset
|
199 This is a convenience function that calls the timeit() |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
200 repeatedly, returning a list of results. The first argument |
|
28531
1639f635d983
correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents:
28363
diff
changeset
|
201 specifies how many times to call timeit(), defaulting to 3; |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
202 the second argument specifies the timer argument, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
203 to one million. |
|
28200
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
204 |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
205 Note: it's tempting to calculate mean and standard deviation |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
206 from the result vector and report these. However, this is not |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
207 very useful. In a typical case, the lowest value gives a |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
208 lower bound for how fast your machine can run the given code |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
209 snippet; higher values in the result vector are typically not |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
210 caused by variability in Python's speed, but by other |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
211 processes interfering with your timing accuracy. So the min() |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
212 of the result is probably the only number you should be |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
213 interested in. After that, you should look at the entire |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
214 vector and apply common sense rather than statistics. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
215 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 r = [] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 for i in range(repeat): |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 t = self.timeit(number) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 r.append(t) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 return r |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
222 def timeit(stmt="pass", setup="pass", timer=default_timer, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
223 number=default_number): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
224 """Convenience function to create Timer object and call timeit method.""" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
225 return Timer(stmt, setup, timer).timeit(number) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
226 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
227 def repeat(stmt="pass", setup="pass", timer=default_timer, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
228 repeat=default_repeat, number=default_number): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
229 """Convenience function to create Timer object and call repeat method.""" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
230 return Timer(stmt, setup, timer).repeat(repeat, number) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
231 |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
232 def main(args=None, *, _wrap_timer=None): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
233 """Main program, used when run as a script. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
234 |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
235 The optional 'args' argument specifies the command line to be parsed, |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
236 defaulting to sys.argv[1:]. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
237 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
238 The return value is an exit code to be passed to sys.exit(); it |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
239 may be None to indicate success. |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
240 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
241 When an exception happens during timing, a traceback is printed to |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
242 stderr and the return value is 1. Exceptions at other times |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
243 (including the template compilation) are not caught. |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
244 |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
245 '_wrap_timer' is an internal interface used for unit testing. If it |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
246 is not None, it must be a callable that accepts a timer function |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
247 and returns another timer function (used for unit testing). |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
248 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 if args is None: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 args = sys.argv[1:] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 import getopt |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 try: |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
253 opts, args = getopt.getopt(args, "n:s:r:tcpvh", |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 ["number=", "setup=", "repeat=", |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
255 "time", "clock", "process", |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
256 "verbose", "help"]) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
257 except getopt.error as err: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
258 print(err) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
259 print("use -h/--help for command line help") |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 return 2 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 timer = default_timer |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 stmt = "\n".join(args) or "pass" |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 number = 0 # auto-determine |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
264 setup = [] |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
265 repeat = default_repeat |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
266 verbose = 0 |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
267 precision = 3 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 for o, a in opts: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 if o in ("-n", "--number"): |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 number = int(a) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 if o in ("-s", "--setup"): |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
272 setup.append(a) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 if o in ("-r", "--repeat"): |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 repeat = int(a) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 if repeat <= 0: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 repeat = 1 |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
277 if o in ("-t", "--time"): |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 timer = time.time |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
279 if o in ("-c", "--clock"): |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 timer = time.clock |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
281 if o in ("-p", "--process"): |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
282 timer = time.process_time |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
283 if o in ("-v", "--verbose"): |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
284 if verbose: |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
285 precision += 1 |
|
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
286 verbose += 1 |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
287 if o in ("-h", "--help"): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
288 print(__doc__, end=' ') |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
289 return 0 |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
290 setup = "\n".join(setup) or "pass" |
|
29178
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
291 # Include the current directory, so that local imports work (sys.path |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
292 # contains the directory of this script, rather than the current |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
293 # directory) |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
294 import os |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
295 sys.path.insert(0, os.curdir) |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
296 if _wrap_timer is not None: |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
297 timer = _wrap_timer(timer) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 t = Timer(stmt, setup, timer) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 if number == 0: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 # determine number so that 0.2 <= total time < 2.0 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 for i in range(1, 10): |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 number = 10**i |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
303 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
304 x = t.timeit(number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
305 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
306 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
307 return 1 |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
308 if verbose: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
309 print("%d loops -> %.*g secs" % (number, precision, x)) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 if x >= 0.2: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 break |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
312 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
313 r = t.repeat(repeat, number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
314 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
315 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
316 return 1 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 best = min(r) |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
318 if verbose: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
319 print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
320 print("%d loops," % number, end=' ') |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 usec = best * 1e6 / number |
|
30727
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
322 if usec < 1000: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
323 print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) |
|
30727
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
324 else: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
325 msec = usec / 1000 |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
326 if msec < 1000: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
327 print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) |
|
30727
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
328 else: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
329 sec = msec / 1000 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
330 print("best of %d: %.*g sec per loop" % (repeat, precision, sec)) |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
331 return None |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 if __name__ == "__main__": |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 sys.exit(main()) |
