Issue #14428: Use the new time.perf_counter() and time.process_time() functions

* Replace "time.clock on windows, or time.time" with time.perf_counter()
 * profile module: only use time.process_time() instead of trying different
   functions providing the process time
 * timeit module: use time.perf_counter() by default, time.time() and
   time.clock() can still be used using --time and --clock options
 * pybench program: use time.perf_counter() by default, add support for
   the new time.process_time() and time.perf_counter() functions, but stay
   backward compatible. Use also time.get_clock_info() to display information
   of the timer.
This commit is contained in:
Victor Stinner 2012-04-29 03:01:20 +02:00
parent 47620a6611
commit fe98e2fc83
7 changed files with 32 additions and 55 deletions

View file

@ -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,18 @@ python pybench.py -s p25.pybench -c p21.pybench
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:])
print('* timer: resolution=%s, implementation=%s'
% (info.resolution, info.implementation))
print()