diff options
Diffstat (limited to 'Lib/timeit.py')
-rwxr-xr-x | Lib/timeit.py | 62 |
1 files changed, 29 insertions, 33 deletions
diff --git a/Lib/timeit.py b/Lib/timeit.py index 4f7d28fbef..0b1c601c97 100755 --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -14,7 +14,8 @@ Command line usage: Options: -n/--number N: how many times to execute 'statement' (default: see below) -r/--repeat N: how many times to repeat the timer (default 3) - -s/--setup S: statement to be executed once initially (default 'pass') + -s/--setup S: statement to be executed once initially (default 'pass'). + Execution time of this setup statement is NOT timed. -p/--process: use time.process_time() (default is time.perf_counter()) -t/--time: use time.time() (deprecated) -c/--clock: use time.clock() (deprecated) @@ -31,38 +32,29 @@ treated similarly. If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds. -The difference in default timer function is because on Windows, -clock() has microsecond granularity but time()'s granularity is 1/60th -of a second; on Unix, clock() has 1/100th of a second granularity and -time() is much more precise. On either platform, the default timer -functions measure wall clock time, not the CPU time. This means that -other processes running on the same computer may interfere with the -timing. The best thing to do when accurate timing is necessary is to -repeat the timing a few times and use the best time. The -r option is -good for this; the default of 3 repetitions is probably enough in most -cases. On Unix, you can use clock() to measure CPU time. - Note: there is a certain baseline overhead associated with executing a -pass statement. The code here doesn't try to hide it, but you should -be aware of it. The baseline overhead can be measured by invoking the -program without arguments. - -The baseline overhead differs between Python versions! Also, to -fairly compare older Python versions to Python 2.3, you may want to -use python -O for the older versions to avoid timing SET_LINENO -instructions. +pass statement. It differs between versions. The code here doesn't try +to hide it, but you should be aware of it. The baseline overhead can be +measured by invoking the program without arguments. + +Classes: + + Timer + +Functions: + + timeit(string, string) -> float + repeat(string, string) -> list + default_timer() -> float + """ import gc import sys import time -try: - import itertools -except ImportError: - # Must be an older Python version (see timeit() below) - itertools = None +import itertools -__all__ = ["Timer"] +__all__ = ["Timer", "timeit", "repeat", "default_timer"] dummy_src_name = "<timeit-src>" default_number = 1000000 @@ -73,7 +65,7 @@ default_timer = time.perf_counter # in Timer.__init__() depend on setup being indented 4 spaces and stmt # being indented 8 spaces. template = """ -def inner(_it, _timer): +def inner(_it, _timer{init}): {setup} _t0 = _timer() for _i in _it: @@ -118,12 +110,19 @@ class Timer: self.timer = timer ns = {} if isinstance(stmt, str): + # Check that the code can be compiled outside a function + if isinstance(setup, str): + compile(setup, dummy_src_name, "exec") + compile(setup + '\n' + stmt, dummy_src_name, "exec") + else: + compile(stmt, dummy_src_name, "exec") stmt = reindent(stmt, 8) if isinstance(setup, str): setup = reindent(setup, 4) - src = template.format(stmt=stmt, setup=setup) + src = template.format(stmt=stmt, setup=setup, init='') elif callable(setup): - src = template.format(stmt=stmt, setup='_setup()') + src = template.format(stmt=stmt, setup='_setup()', + init=', _setup=_setup') ns['_setup'] = setup else: raise ValueError("setup is neither a string nor callable") @@ -180,10 +179,7 @@ class Timer: to one million. The main statement, the setup statement and the timer function to be used are passed to the constructor. """ - if itertools: - it = itertools.repeat(None, number) - else: - it = [None] * number + it = itertools.repeat(None, number) gcold = gc.isenabled() gc.disable() try: |