annotate Lib/timeit.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 57ee0793e462
children 84363c747c21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:
28184
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
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
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 statement: statement to be timed (default 'pass')
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
23
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
24 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
25 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
26 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
27 treated similarly.
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
28
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
29 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
30 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
31
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
32 The difference in default timer function is because on Windows,
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
43 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
44 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
45 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
46 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
47
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
48 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
49 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
50 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
51 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
52 """
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
53
31340
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
54 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
55 import sys
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 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
57 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
58 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
59 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
60 # 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
61 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
62
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 __all__ = ["Timer"]
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
65 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
66 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
67 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
68
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 # 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
71 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
72 else:
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # 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
74 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
75
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
76 # 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
77 # 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
78 # 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
79 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
80 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
81 %(setup)s
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
82 _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
83 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
84 %(stmt)s
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
85 _t1 = _timer()
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
86 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
87 """
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 def reindent(src, indent):
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
90 """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
91 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
92
41165
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
93 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
94 """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
95 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
96 setup()
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
97 _t0 = _timer()
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
98 for _i in _it:
52757
183d6072e1e5 Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
99 _func()
41165
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
100 _t1 = _timer()
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
101 return _t1 - _t0
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
102 return inner
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
103
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 class Timer:
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
105 """Class for timing execution speed of small code snippets.
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
106
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
107 The constructor takes a statement to be timed, an additional
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
108 statement used for setup, and a timer function. Both statements
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
109 default to 'pass'; the timer function is platform-dependent (see
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
110 module doc string).
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 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
113 timeit() method. The repeat() method is a convenience to call
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
114 timeit() multiple times and return a list of results.
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
115
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
116 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
117 multi-line string literals.
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
118 """
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 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
121 """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
122 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
123 ns = {}
41165
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
124 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
125 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
126 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
127 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
128 src = template % {'stmt': stmt, 'setup': setup}
56435
57ee0793e462 replace callable()
Benjamin Peterson <benjamin@python.org>
parents: 52757
diff changeset
129 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
130 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
131 ns['_setup'] = setup
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
132 else:
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
133 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
134 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
135 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
136 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
137 self.inner = ns["inner"]
56435
57ee0793e462 replace callable()
Benjamin Peterson <benjamin@python.org>
parents: 52757
diff changeset
138 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
139 self.src = None
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
140 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
141 _setup = setup
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
142 def setup():
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
143 exec _setup in globals(), ns
56435
57ee0793e462 replace callable()
Benjamin Peterson <benjamin@python.org>
parents: 52757
diff changeset
144 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
145 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
146 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
147 else:
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
148 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
149
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
150 def print_exc(self, file=None):
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
151 """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
152
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
153 Typical use:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
154
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
155 t = Timer(...) # outside the try/except
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
156 try:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
157 t.timeit(...) # or t.repeat(...)
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
158 except:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
159 t.print_exc()
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
160
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
161 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
162 in the compiled template will be displayed.
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
163
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
164 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
165 sent; it defaults to sys.stderr.
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
166 """
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
167 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
168 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
169 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
170 None,
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
171 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
172 dummy_src_name)
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
173 # 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
174
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
175 traceback.print_exc(file=file)
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
176
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 def timeit(self, number=default_number):
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
178 """Time 'number' executions of the main statement.
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
179
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
180 To be precise, this executes the setup statement once, and
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
181 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
182 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
183 argument is the number of times through the loop, defaulting
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
184 to one million. The main statement, the setup statement and
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
185 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
186 """
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
187 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
188 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
189 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
190 it = [None] * number
31340
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
191 gcold = gc.isenabled()
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
192 gc.disable()
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
193 timing = self.inner(it, self.timer)
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
194 if gcold:
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
195 gc.enable()
e8dff11e9358 SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents: 30727
diff changeset
196 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
197
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 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
199 """Call timeit() a few times.
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
200
28531
1639f635d983 correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents: 28363
diff changeset
201 This is a convenience function that calls the timeit()
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
202 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
203 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
204 the second argument specifies the timer argument, defaulting
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
205 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
206
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 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 vector and apply common sense rather than statistics.
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
217 """
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 r = []
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 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
220 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
221 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
222 return r
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
41165
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
224 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
225 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
226 """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
227 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
228
d96ebb20d3e1 Patch #1533909: the timeit module now accepts callables in addition to
Georg Brandl <georg@python.org>
parents: 31340
diff changeset
229 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
230 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
231 """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
232 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
233
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 def main(args=None):
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
235 """Main program, used when run as a script.
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
236
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
237 The optional argument specifies the command line to be parsed,
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
238 defaulting to sys.argv[1:].
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 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
241 may be None to indicate success.
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
242
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
243 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
244 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
245 (including the template compilation) are not caught.
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
246 """
28181
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 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
248 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
249 import getopt
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 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
251 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
252 ["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
253 "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
254 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
255 print err
28184
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
256 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
257 return 2
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273 repeat = 1
28184
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
274 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
275 timer = time.time
28184
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
276 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
277 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
278 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
279 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
280 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
281 verbose += 1
28184
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
282 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
283 print __doc__,
736ad73df5b4 Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents: 28183
diff changeset
284 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
285 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
286 # 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
287 # 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
288 # directory)
1dc7c0a020d3 SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents: 28533
diff changeset
289 import os
1dc7c0a020d3 SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents: 28533
diff changeset
290 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
291 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
292 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
293 # 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
294 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
295 number = 10**i
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
296 try:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
297 x = t.timeit(number)
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
298 except:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
299 t.print_exc()
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
300 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
301 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
302 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
303 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
304 break
28298
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
305 try:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
306 r = t.repeat(repeat, number)
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
307 except:
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
308 t.print_exc()
abfe4b380305 Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents: 28213
diff changeset
309 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
310 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
311 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
312 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
313 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
314 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
315 if usec < 1000:
ad5822977152 Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents: 30464
diff changeset
316 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
317 else:
ad5822977152 Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents: 30464
diff changeset
318 msec = usec / 1000
ad5822977152 Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents: 30464
diff changeset
319 if msec < 1000:
ad5822977152 Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents: 30464
diff changeset
320 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
321 else:
ad5822977152 Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents: 30464
diff changeset
322 sec = 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 sec per loop" % (repeat, precision, sec)
28183
92f1d02e28f0 Added more documentation.
Guido van Rossum <guido@python.org>
parents: 28181
diff changeset
324 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
325
cd21be82f50b A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 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
327 sys.exit(main())