Mercurial > cpython-lru_cache-weakref
annotate Lib/timeit.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 56e7b42089c8 |
| children |
| rev | line source |
|---|---|
|
28533
f213ee6ce55e
add a #! line for unix weenies
Skip Montanaro <skip@pobox.com>
parents:
28531
diff
changeset
|
1 #! /usr/bin/env python |
|
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: |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52757
diff
changeset
|
12 python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-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') |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 -t/--time: use time.time() (default on Unix) |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 -c/--clock: use time.clock() (default on Windows) |
|
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
|
20 -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
|
21 -h/--help: print this usage message and exit |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52757
diff
changeset
|
22 --: 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
|
23 statement: statement to be timed (default 'pass') |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
24 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 treated similarly. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
29 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
30 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
|
31 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
|
32 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
33 The difference in default timer function is because on Windows, |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
|
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 |
|
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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 """ |
|
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
|
54 |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
55 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
|
56 import sys |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 # 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
|
62 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
|
63 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 __all__ = ["Timer"] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
66 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
|
67 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
|
68 default_repeat = 3 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 if sys.platform == "win32": |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 # On Windows, the best timer is time.clock() |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 default_timer = time.clock |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 else: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 # On most other platforms the best timer is time.time() |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 default_timer = time.time |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
77 # 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
|
78 # 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
|
79 # 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
|
80 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
|
81 def inner(_it, _timer): |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 %(setup)s |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
83 _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
|
84 for _i in _it: |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 %(stmt)s |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
86 _t1 = _timer() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
87 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
|
88 """ |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 def reindent(src, indent): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
91 """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
|
92 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
|
93 |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
94 def _template_func(setup, func): |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
95 """Create a timer function. Used if the "statement" is a callable.""" |
|
52757
183d6072e1e5
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
41532
diff
changeset
|
96 def inner(_it, _timer, _func=func): |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
97 setup() |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
98 _t0 = _timer() |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
99 for _i in _it: |
|
52757
183d6072e1e5
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
41532
diff
changeset
|
100 _func() |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
101 _t1 = _timer() |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
102 return _t1 - _t0 |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
103 return inner |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
104 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 class Timer: |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
106 """Class for timing execution speed of small code snippets. |
|
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 The constructor takes a statement to be timed, an additional |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
109 statement used for setup, and a timer function. Both statements |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
110 default to 'pass'; the timer function is platform-dependent (see |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
111 module doc string). |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
112 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
113 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
|
114 timeit() method. The repeat() method is a convenience to call |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
115 timeit() multiple times and return a list of results. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
116 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
117 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
|
118 multi-line string literals. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
119 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 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
|
122 """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
|
123 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
|
124 ns = {} |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
125 if isinstance(stmt, basestring): |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
126 stmt = reindent(stmt, 8) |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
127 if isinstance(setup, basestring): |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
128 setup = reindent(setup, 4) |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
129 src = template % {'stmt': stmt, 'setup': setup} |
|
56435
57ee0793e462
replace callable()
Benjamin Peterson <benjamin@python.org>
parents:
52757
diff
changeset
|
130 elif hasattr(setup, '__call__'): |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
131 src = template % {'stmt': stmt, 'setup': '_setup()'} |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
132 ns['_setup'] = setup |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
133 else: |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
134 raise ValueError("setup is neither a string nor callable") |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
135 self.src = src # Save for traceback display |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
136 code = compile(src, dummy_src_name, "exec") |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
137 exec code in globals(), ns |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
138 self.inner = ns["inner"] |
|
56435
57ee0793e462
replace callable()
Benjamin Peterson <benjamin@python.org>
parents:
52757
diff
changeset
|
139 elif hasattr(stmt, '__call__'): |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
140 self.src = None |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
141 if isinstance(setup, basestring): |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
142 _setup = setup |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
143 def setup(): |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
144 exec _setup in globals(), ns |
|
56435
57ee0793e462
replace callable()
Benjamin Peterson <benjamin@python.org>
parents:
52757
diff
changeset
|
145 elif not hasattr(setup, '__call__'): |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
146 raise ValueError("setup is neither a string nor callable") |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
147 self.inner = _template_func(setup, stmt) |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
148 else: |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
149 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
|
150 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
151 def print_exc(self, file=None): |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
152 """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
|
153 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
154 Typical use: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
155 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
156 t = Timer(...) # outside the try/except |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
157 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
158 t.timeit(...) # or t.repeat(...) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
159 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
160 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
161 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
162 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
|
163 in the compiled template will be displayed. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
164 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
165 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
|
166 sent; it defaults to sys.stderr. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
167 """ |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
168 import linecache, traceback |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
169 if self.src is not None: |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
170 linecache.cache[dummy_src_name] = (len(self.src), |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
171 None, |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
172 self.src.split("\n"), |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
173 dummy_src_name) |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
174 # else the source is already stored somewhere else |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
175 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
176 traceback.print_exc(file=file) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
177 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 def timeit(self, number=default_number): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
179 """Time 'number' executions of the main statement. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
180 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
181 To be precise, this executes the setup statement once, and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
182 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
|
183 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
|
184 argument is the number of times through the loop, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
185 to one million. The main statement, the setup statement and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
186 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
|
187 """ |
|
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
|
188 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
|
189 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
|
190 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
|
191 it = [None] * number |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
192 gcold = gc.isenabled() |
|
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
193 gc.disable() |
|
71546
56e7b42089c8
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
194 try: |
|
56e7b42089c8
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
195 timing = self.inner(it, self.timer) |
|
56e7b42089c8
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
196 finally: |
|
56e7b42089c8
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
197 if gcold: |
|
56e7b42089c8
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
198 gc.enable() |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
199 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
|
200 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 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
|
202 """Call timeit() a few times. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
203 |
|
28531
1639f635d983
correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents:
28363
diff
changeset
|
204 This is a convenience function that calls the timeit() |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
205 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
|
206 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
|
207 the second argument specifies the timer argument, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
208 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
|
209 |
|
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 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 vector and apply common sense rather than statistics. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
220 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 r = [] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 return r |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
227 def timeit(stmt="pass", setup="pass", timer=default_timer, |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
228 number=default_number): |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41165
diff
changeset
|
229 """Convenience function to create Timer object and call timeit method.""" |
|
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41165
diff
changeset
|
230 return Timer(stmt, setup, timer).timeit(number) |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
231 |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
232 def repeat(stmt="pass", setup="pass", timer=default_timer, |
|
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
233 repeat=default_repeat, number=default_number): |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41165
diff
changeset
|
234 """Convenience function to create Timer object and call repeat method.""" |
|
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41165
diff
changeset
|
235 return Timer(stmt, setup, timer).repeat(repeat, number) |
|
41165
d96ebb20d3e1
Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents:
31340
diff
changeset
|
236 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 def main(args=None): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
238 """Main program, used when run as a script. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
239 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
240 The optional argument specifies the command line to be parsed, |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
241 defaulting to sys.argv[1:]. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
242 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
243 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
|
244 may be None to indicate success. |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
245 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
246 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
|
247 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
|
248 (including the template compilation) are not caught. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
249 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 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
|
251 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
|
252 import getopt |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 try: |
|
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
|
254 opts, args = getopt.getopt(args, "n:s:r:tcvh", |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 ["number=", "setup=", "repeat=", |
|
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
|
256 "time", "clock", "verbose", "help"]) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 except getopt.error, err: |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 print err |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
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 |
|
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
|
281 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
|
282 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
|
283 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
|
284 verbose += 1 |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
285 if o in ("-h", "--help"): |
|
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
286 print __doc__, |
|
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
287 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
|
288 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
|
289 # 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
|
290 # 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
|
291 # directory) |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
292 import os |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
293 sys.path.insert(0, os.curdir) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 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
|
295 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
|
296 # 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
|
297 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
|
298 number = 10**i |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
299 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
300 x = t.timeit(number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
301 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
302 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 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
|
307 break |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
308 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
309 r = t.repeat(repeat, number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
310 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
311 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
312 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
|
313 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
|
314 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
|
315 print "raw times:", " ".join(["%.*g" % (precision, x) for x in r]) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 print "%d loops," % number, |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 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
|
318 if usec < 1000: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
319 print "best of %d: %.*g usec per loop" % (repeat, precision, usec) |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
320 else: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
321 msec = usec / 1000 |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
322 if msec < 1000: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
323 print "best of %d: %.*g msec per loop" % (repeat, precision, msec) |
|
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 sec = msec / 1000 |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
326 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
|
327 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
|
328 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 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
|
330 sys.exit(main()) |
