Mercurial > cpython-lru_cache-weakref
annotate Lib/timeit.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | dda8a6b8a351 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
52758
diff
changeset
|
1 #! /usr/bin/env python3 |
|
28533
f213ee6ce55e
add a #! line for unix weenies
Skip Montanaro <skip@pobox.com>
parents:
28531
diff
changeset
|
2 |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
3 """Tool for measuring execution time of small code snippets. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
5 This module avoids a number of common traps for measuring execution |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
6 times. See also Tim Peters' introduction to the Algorithms chapter in |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
7 the Python Cookbook, published by O'Reilly. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
9 Library usage: see the Timer class. |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 Command line usage: |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
12 python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement] |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 Options: |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
15 -n/--number N: how many times to execute 'statement' (default: see below) |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
16 -r/--repeat N: how many times to repeat the timer (default 3) |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
17 -s/--setup S: statement to be executed once initially (default 'pass') |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
18 -p/--process: use time.process_time() (default is time.perf_counter()) |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
19 -t/--time: use time.time() (deprecated) |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
20 -c/--clock: use time.clock() (deprecated) |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
21 -v/--verbose: print raw timing results; repeat for more digits precision |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
22 -h/--help: print this usage message and exit |
|
63438
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52758
diff
changeset
|
23 --: separate options from statement, use when statement starts with - |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 statement: statement to be timed (default 'pass') |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
25 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
26 A multi-line statement may be given by specifying each line as a |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
27 separate argument; indented lines are possible by enclosing an |
|
28213
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
28 argument in quotes and using leading spaces. Multiple -s options are |
|
c03be22fdd24
Broke down and made it work for Python 2.0 and up. (Older versions
Guido van Rossum <guido@python.org>
parents:
28200
diff
changeset
|
29 treated similarly. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
30 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
31 If -n is not given, a suitable number of loops is calculated by trying |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
32 successive powers of 10 until the total time is at least 0.2 seconds. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
33 |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
34 Note: there is a certain baseline overhead associated with executing a |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
35 pass statement. It differs between versions. The code here doesn't try |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
36 to hide it, but you should be aware of it. The baseline overhead can be |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
37 measured by invoking the program without arguments. |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
38 |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
39 Classes: |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
40 |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
41 Timer |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
42 |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
43 Functions: |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
45 timeit(string, string) -> float |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
46 repeat(string, string) -> list |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
47 default_timer() -> float |
|
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
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 """ |
|
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
|
50 |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
51 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
|
52 import sys |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 import time |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
54 import itertools |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
56 __all__ = ["Timer", "timeit", "repeat", "default_timer"] |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
58 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
|
59 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
|
60 default_repeat = 3 |
|
76599
bd195749c0a2
Issue #14428: Use the new time.perf_counter() and time.process_time() functions
Victor Stinner <victor.stinner@gmail.com>
parents:
73166
diff
changeset
|
61 default_timer = time.perf_counter |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
63 # 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
|
64 # 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
|
65 # 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
|
66 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
|
67 def inner(_it, _timer): |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
68 {setup} |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
69 _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
|
70 for _i in _it: |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
71 {stmt} |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
72 _t1 = _timer() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
73 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
|
74 """ |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 def reindent(src, indent): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
77 """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
|
78 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
|
79 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
80 def _template_func(setup, func): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
81 """Create a timer function. Used if the "statement" is a callable.""" |
|
52758
1bf671217abf
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
43577
diff
changeset
|
82 def inner(_it, _timer, _func=func): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
83 setup() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
84 _t0 = _timer() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
85 for _i in _it: |
|
52758
1bf671217abf
Localize the function lookup in timeit.
Raymond Hettinger <python@rcn.com>
parents:
43577
diff
changeset
|
86 _func() |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
87 _t1 = _timer() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
88 return _t1 - _t0 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
89 return inner |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
90 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 class Timer: |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
92 """Class for timing execution speed of small code snippets. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
93 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
94 The constructor takes a statement to be timed, an additional |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
95 statement used for setup, and a timer function. Both statements |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
96 default to 'pass'; the timer function is platform-dependent (see |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
97 module doc string). |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
98 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
99 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
|
100 timeit() method. The repeat() method is a convenience to call |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
101 timeit() multiple times and return a list of results. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
102 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
103 The 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
|
104 multi-line string literals. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
105 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 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
|
108 """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
|
109 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
|
110 ns = {} |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
111 if isinstance(stmt, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
112 stmt = reindent(stmt, 8) |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
113 if isinstance(setup, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
114 setup = reindent(setup, 4) |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
115 src = template.format(stmt=stmt, setup=setup) |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
116 elif callable(setup): |
|
69131
81c981ceb83e
Update timeit to use the new string formatting syntax.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
117 src = template.format(stmt=stmt, setup='_setup()') |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
118 ns['_setup'] = setup |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
119 else: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
120 raise ValueError("setup is neither a string nor callable") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
121 self.src = src # Save for traceback display |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
122 code = compile(src, dummy_src_name, "exec") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
123 exec(code, globals(), ns) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
124 self.inner = ns["inner"] |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
125 elif callable(stmt): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
126 self.src = None |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
127 if isinstance(setup, str): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
128 _setup = setup |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
129 def setup(): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
130 exec(_setup, globals(), ns) |
|
73165
8e57b5d8f58f
Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71547
diff
changeset
|
131 elif not callable(setup): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
132 raise ValueError("setup is neither a string nor callable") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
133 self.inner = _template_func(setup, stmt) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
134 else: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
135 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
|
136 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
137 def print_exc(self, file=None): |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
138 """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
|
139 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
140 Typical use: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
141 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
142 t = Timer(...) # outside the try/except |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
143 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
144 t.timeit(...) # or t.repeat(...) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
145 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
146 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
147 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
148 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
|
149 in the compiled template will be displayed. |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
150 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
151 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
|
152 sent; it defaults to sys.stderr. |
|
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 import linecache, traceback |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
155 if self.src is not None: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
156 linecache.cache[dummy_src_name] = (len(self.src), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
157 None, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
158 self.src.split("\n"), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
159 dummy_src_name) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
160 # else the source is already stored somewhere else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
161 |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
162 traceback.print_exc(file=file) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
163 |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 def timeit(self, number=default_number): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
165 """Time 'number' executions of the main statement. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
166 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
167 To be precise, this executes the setup statement once, and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
168 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
|
169 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
|
170 argument is the number of times through the loop, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
171 to one million. The main statement, the setup statement and |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
172 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
|
173 """ |
|
82665
dda8a6b8a351
Issue #17414: Add timeit, repeat, and default_timer to timeit.__all__.
Terry Jan Reedy <tjreedy@udel.edu>
parents:
76692
diff
changeset
|
174 it = itertools.repeat(None, number) |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
175 gcold = gc.isenabled() |
|
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
176 gc.disable() |
|
71547
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
177 try: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
178 timing = self.inner(it, self.timer) |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
179 finally: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
180 if gcold: |
|
1cdb281a78ce
Issue 12514: Use try/finally to assure that timeit restores GC when done.
Raymond Hettinger <python@rcn.com>
parents:
68623
diff
changeset
|
181 gc.enable() |
|
31340
e8dff11e9358
SF patch #868736: Disable GC for timeit
Raymond Hettinger <python@rcn.com>
parents:
30727
diff
changeset
|
182 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
|
183 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 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
|
185 """Call timeit() a few times. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
186 |
|
28531
1639f635d983
correct a couple docstring nits
Skip Montanaro <skip@pobox.com>
parents:
28363
diff
changeset
|
187 This is a convenience function that calls the timeit() |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
188 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
|
189 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
|
190 the second argument specifies the timer argument, defaulting |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
191 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
|
192 |
|
0749787994db
Add a note explaining why you shouldn't try to compute mean and
Guido van Rossum <guido@python.org>
parents:
28197
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 vector and apply common sense rather than statistics. |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
203 """ |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 r = [] |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 return r |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
210 def timeit(stmt="pass", setup="pass", timer=default_timer, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
211 number=default_number): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
212 """Convenience function to create Timer object and call timeit method.""" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
213 return Timer(stmt, setup, timer).timeit(number) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
214 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
215 def repeat(stmt="pass", setup="pass", timer=default_timer, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
216 repeat=default_repeat, number=default_number): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
217 """Convenience function to create Timer object and call repeat method.""" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
218 return Timer(stmt, setup, timer).repeat(repeat, number) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
219 |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
220 def main(args=None, *, _wrap_timer=None): |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
221 """Main program, used when run as a script. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
222 |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
223 The optional 'args' argument specifies the command line to be parsed, |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
224 defaulting to sys.argv[1:]. |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
225 |
|
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
diff
changeset
|
226 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
|
227 may be None to indicate success. |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
228 |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
229 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
|
230 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
|
231 (including the template compilation) are not caught. |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
232 |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
233 '_wrap_timer' is an internal interface used for unit testing. If it |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
234 is not None, it must be a callable that accepts a timer function |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
235 and returns another timer function (used for unit testing). |
|
28183
92f1d02e28f0
Added more documentation.
Guido van Rossum <guido@python.org>
parents:
28181
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 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
|
238 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
|
239 import getopt |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 try: |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
241 opts, args = getopt.getopt(args, "n:s:r:tcpvh", |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 ["number=", "setup=", "repeat=", |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
243 "time", "clock", "process", |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
244 "verbose", "help"]) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39782
diff
changeset
|
245 except getopt.error as err: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
246 print(err) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
247 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
|
248 return 2 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 repeat = 1 |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
265 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
|
266 timer = time.time |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
267 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
|
268 timer = time.clock |
|
76692
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
269 if o in ("-p", "--process"): |
|
d43a8aa9dbef
Add an option to timeit to use time.process_time() and mark -t and -c as deprecated.
Georg Brandl <georg@python.org>
parents:
76599
diff
changeset
|
270 timer = time.process_time |
|
28301
73434fa9d285
Change the default number of repetitions to 3, both in the Timer class
Guido van Rossum <guido@python.org>
parents:
28298
diff
changeset
|
271 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
|
272 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
|
273 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
|
274 verbose += 1 |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
275 if o in ("-h", "--help"): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
276 print(__doc__, end=' ') |
|
28184
736ad73df5b4
Add notes about baseline overhead, and about different Python
Guido van Rossum <guido@python.org>
parents:
28183
diff
changeset
|
277 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
|
278 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
|
279 # 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
|
280 # 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
|
281 # directory) |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
282 import os |
|
1dc7c0a020d3
SF bug 735293: Command line timeit.py sets sys.path badly
Raymond Hettinger <python@rcn.com>
parents:
28533
diff
changeset
|
283 sys.path.insert(0, os.curdir) |
|
68623
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
284 if _wrap_timer is not None: |
|
628a3679dc14
#11578: add unit tests for timeit module.
R David Murray <rdmurray@bitdance.com>
parents:
68257
diff
changeset
|
285 timer = _wrap_timer(timer) |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 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
|
287 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
|
288 # 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
|
289 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
|
290 number = 10**i |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
291 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
292 x = t.timeit(number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
293 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
294 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
295 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
|
296 if verbose: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
297 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
|
298 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
|
299 break |
|
28298
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
300 try: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
301 r = t.repeat(repeat, number) |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
302 except: |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
303 t.print_exc() |
|
abfe4b380305
Implement some recommendations from Raymond H:
Guido van Rossum <guido@python.org>
parents:
28213
diff
changeset
|
304 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
|
305 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
|
306 if verbose: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
307 print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
308 print("%d loops," % number, end=' ') |
|
28181
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 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
|
310 if usec < 1000: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
311 print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) |
|
30727
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
312 else: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
313 msec = usec / 1000 |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
314 if msec < 1000: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
315 print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) |
|
30727
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
316 else: |
|
ad5822977152
Show microseconds, milliseconds or seconds, whichever is most natural,
Guido van Rossum <guido@python.org>
parents:
30464
diff
changeset
|
317 sec = msec / 1000 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
318 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
|
319 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
|
320 |
|
cd21be82f50b
A flexible utility to time the execution speed of a code snippet.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 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
|
322 sys.exit(main()) |
