diff options
Diffstat (limited to 'Lib/timeit.py')
-rw-r--r-- | Lib/timeit.py | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/Lib/timeit.py b/Lib/timeit.py index 1ae59e0..4f7d28f 100644 --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -9,14 +9,15 @@ the Python Cookbook, published by O'Reilly. Library usage: see the Timer class. Command line usage: - python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [--] [statement] + python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-p] [-h] [--] [statement] 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') - -t/--time: use time.time() (default on Unix) - -c/--clock: use time.clock() (default on Windows) + -p/--process: use time.process_time() (default is time.perf_counter()) + -t/--time: use time.time() (deprecated) + -c/--clock: use time.clock() (deprecated) -v/--verbose: print raw timing results; repeat for more digits precision -h/--help: print this usage message and exit --: separate options from statement, use when statement starts with - @@ -66,23 +67,17 @@ __all__ = ["Timer"] dummy_src_name = "<timeit-src>" default_number = 1000000 default_repeat = 3 - -if sys.platform == "win32": - # On Windows, the best timer is time.clock() - default_timer = time.clock -else: - # On most other platforms the best timer is time.time() - default_timer = time.time +default_timer = time.perf_counter # Don't change the indentation of the template; the reindent() calls # in Timer.__init__() depend on setup being indented 4 spaces and stmt # being indented 8 spaces. template = """ def inner(_it, _timer): - %(setup)s + {setup} _t0 = _timer() for _i in _it: - %(stmt)s + {stmt} _t1 = _timer() return _t1 - _t0 """ @@ -126,9 +121,9 @@ class Timer: stmt = reindent(stmt, 8) if isinstance(setup, str): setup = reindent(setup, 4) - src = template % {'stmt': stmt, 'setup': setup} + src = template.format(stmt=stmt, setup=setup) elif callable(setup): - src = template % {'stmt': stmt, 'setup': '_setup()'} + src = template.format(stmt=stmt, setup='_setup()') ns['_setup'] = setup else: raise ValueError("setup is neither a string nor callable") @@ -255,9 +250,10 @@ def main(args=None, *, _wrap_timer=None): args = sys.argv[1:] import getopt try: - opts, args = getopt.getopt(args, "n:s:r:tcvh", + opts, args = getopt.getopt(args, "n:s:r:tcpvh", ["number=", "setup=", "repeat=", - "time", "clock", "verbose", "help"]) + "time", "clock", "process", + "verbose", "help"]) except getopt.error as err: print(err) print("use -h/--help for command line help") @@ -282,6 +278,8 @@ def main(args=None, *, _wrap_timer=None): timer = time.time if o in ("-c", "--clock"): timer = time.clock + if o in ("-p", "--process"): + timer = time.process_time if o in ("-v", "--verbose"): if verbose: precision += 1 |