diff -r 384190bb0bd5 Doc/includes/mp_benchmarks.py --- a/Doc/includes/mp_benchmarks.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Doc/includes/mp_benchmarks.py Thu Apr 19 00:03:06 2012 +0200 @@ -6,16 +6,12 @@ # import time -import sys import multiprocessing import threading import queue import gc -if sys.platform == 'win32': - _timer = time.clock -else: - _timer = time.time +_timer = time.perf_counter delta = 1 diff -r 384190bb0bd5 Lib/idlelib/ColorDelegator.py --- a/Lib/idlelib/ColorDelegator.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Lib/idlelib/ColorDelegator.py Thu Apr 19 00:03:06 2012 +0200 @@ -149,9 +149,9 @@ class ColorDelegator(Delegator): self.stop_colorizing = False self.colorizing = True if DEBUG: print("colorizing...") - t0 = time.clock() + t0 = time.perf_counter() self.recolorize_main() - t1 = time.clock() + t1 = time.perf_counter() if DEBUG: print("%.3f seconds" % (t1-t0)) finally: self.colorizing = False diff -r 384190bb0bd5 Lib/profile.py --- a/Lib/profile.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Lib/profile.py Thu Apr 19 00:03:06 2012 +0200 @@ -83,26 +83,6 @@ def runctx(statement, globals, locals, f else: return prof.print_stats(sort) -if hasattr(os, "times"): - def _get_time_times(timer=os.times): - t = timer() - return t[0] + t[1] - -# Using getrusage(3) is better than clock(3) if available: -# on some systems (e.g. FreeBSD), getrusage has a higher resolution -# Furthermore, on a POSIX system, returns microseconds, which -# wrap around after 36min. -_has_res = 0 -try: - import resource - resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF) - def _get_time_resource(timer=resgetrusage): - t = timer() - return t[0] + t[1] - _has_res = 1 -except ImportError: - pass - class Profile: """Profiler class. @@ -155,20 +135,8 @@ class Profile: self.bias = bias # Materialize in local dict for lookup speed. if not timer: - if _has_res: - self.timer = resgetrusage - self.dispatcher = self.trace_dispatch - self.get_time = _get_time_resource - elif hasattr(time, 'clock'): - self.timer = self.get_time = time.clock - self.dispatcher = self.trace_dispatch_i - elif hasattr(os, 'times'): - self.timer = os.times - self.dispatcher = self.trace_dispatch - self.get_time = _get_time_times - else: - self.timer = self.get_time = time.time - self.dispatcher = self.trace_dispatch_i + self.timer = self.get_time = time.process_time + self.dispatcher = self.trace_dispatch_i else: self.timer = timer t = self.timer() # test out timer function diff -r 384190bb0bd5 Lib/test/reperf.py --- a/Lib/test/reperf.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Lib/test/reperf.py Thu Apr 19 00:03:06 2012 +0200 @@ -9,13 +9,13 @@ def main(): timefunc(10, p.findall, s) def timefunc(n, func, *args, **kw): - t0 = time.clock() + t0 = time.perf_counter() try: for i in range(n): result = func(*args, **kw) return result finally: - t1 = time.clock() + t1 = time.perf_counter() if n > 1: print(n, "times", end=' ') print(func.__name__, "%.3f" % (t1-t0), "CPU seconds") diff -r 384190bb0bd5 Lib/test/sortperf.py --- a/Lib/test/sortperf.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Lib/test/sortperf.py Thu Apr 19 00:03:06 2012 +0200 @@ -57,9 +57,9 @@ def flush(): sys.stdout.flush() def doit(L): - t0 = time.clock() + t0 = time.perf_counter() L.sort() - t1 = time.clock() + t1 = time.perf_counter() print("%6.2f" % (t1-t0), end=' ') flush() diff -r 384190bb0bd5 Lib/timeit.py --- a/Lib/timeit.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Lib/timeit.py Thu Apr 19 00:03:06 2012 +0200 @@ -15,8 +15,8 @@ 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) + -t/--time: use time.time() + -c/--clock: use time.clock() -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,13 +66,7 @@ __all__ = ["Timer"] dummy_src_name = "" 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 diff -r 384190bb0bd5 Tools/pybench/pybench.py --- a/Tools/pybench/pybench.py Wed Apr 18 01:19:05 2012 +0200 +++ b/Tools/pybench/pybench.py Thu Apr 19 00:03:06 2012 +0200 @@ -73,11 +73,15 @@ ALLOW_SKIPPING_CALIBRATION = 1 # Timer types TIMER_TIME_TIME = 'time.time' +TIMER_TIME_PROCESS_TIME = 'time.process_time' +TIMER_TIME_PERF_COUNTER = 'time.perf_counter' TIMER_TIME_CLOCK = 'time.clock' TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' # Choose platform default timer -if sys.platform[:3] == 'win': +if hasattr(time, 'perf_counter'): + TIMER_PLATFORM_DEFAULT = TIMER_TIME_PERF_COUNTER +elif sys.platform[:3] == 'win': # On WinXP this has 2.5ms resolution TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK else: @@ -93,6 +97,10 @@ def get_timer(timertype): if timertype == TIMER_TIME_TIME: return time.time + elif timertype == TIMER_TIME_PROCESS_TIME: + return time.process_time + elif timertype == TIMER_TIME_PERF_COUNTER: + return time.perf_counter elif timertype == TIMER_TIME_CLOCK: return time.clock elif timertype == TIMER_SYSTIMES_PROCESSTIME: @@ -866,7 +874,23 @@ python pybench.py -s p25.pybench -c p21. print('* using timer: systimes.processtime (%s)' % \ systimes.SYSTIMES_IMPLEMENTATION) else: + # Check that the clock function does exist + try: + get_timer(timer) + except TypeError: + print("* Error: Unknown timer: %s" % timer) + return + print('* using timer: %s' % timer) + if hasattr(time, 'get_clock_info'): + info = time.get_clock_info(timer[5:]) + text = [] + if 'precision' in info: + text.append('precision=%s' % info['precision']) + else: + text.append('resolution=%s' % info['resolution']) + text.append('implementation=%s' % info['implementation']) + print('* timer: %s' % ', '.join(text)) print()