diff --git a/Lib/timeit.py b/Lib/timeit.py --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -71,8 +71,26 @@ 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 + # Try to find the most appropriate POSIX clock + _clocks = ['CLOCK_PROCESS_CPUTIME_ID', 'CLOCK_MONOTONIC_RAW', + 'CLOCK_MONOTONIC', 'CLOCK_REALTIME'] + for _clock in _clocks: + try: + _clock = getattr(time, _clock) + except AttributeError: + continue + def default_timer(): + return time.clock_gettime(_clock) + try: + default_timer() + except IOError: + # Clock not usuable + continue + break + else: + # On most other platforms the best timer is time.time() + default_timer = time.time + del _clocks # Don't change the indentation of the template; the reindent() calls # in Timer.__init__() depend on setup being indented 4 spaces and stmt