8000 Issue #14428: Use the new time.perf_counter() and time.process_time()… · python/cpython@fe98e2f · GitHub
[go: up one dir, main page]

Skip to content

Commit fe98e2f

Browse files
committed
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.
1 parent 47620a6 commit fe98e2f

File tree

7 files changed

+32
-55
lines changed

7 files changed

+32
-55
lines changed

Doc/includes/mp_benchmarks.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
#
77

88
import time
9-
import sys
109
import multiprocessing
1110
import threading
1211
import queue
1312
import gc
1413

15-
if sys.platform == 'win32':
16-
_timer = time.clock
17-
else:
18-
_timer = time.time
14+
_timer = time.perf_counter
1915

2016
delta = 1
2117

Lib/idlelib/ColorDelegator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ def recolorize(self):
149149
self.stop_colorizing = False
150150
self.colorizing = True
151151
if DEBUG: print("colorizing...")
152-
t0 = time.clock()
152+
t0 = time.perf_counter()
153153
self.recolorize_main()
154-
t1 = time.clock()
154+
t1 = time.perf_counter()
155155
if DEBUG: print("%.3f seconds" % (t1-t0))
156156
finally:
157157
self.colorizing = False

Lib/profile.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,6 @@ def runctx(statement, globals, locals, filename=None, sort=-1):
8383
else:
8484
return prof.print_stats(sort)
8585

86-
if hasattr(os, "times"):
87-
def _get_time_times(timer=os.times):
88-
t = timer()
89-
return t[0] + t[1]
90-
91-
# Using getrusage(3) is better than clock(3) if available:
92-
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
93-
# Furthermore, on a POSIX system, returns microseconds, which
94-
# wrap around after 36min.
95-
_has_res = 0
96-
try:
97-
import resource
98-
resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF)
99-
def _get_time_resource(timer=resgetrusage):
100-
t = timer()
101-
return t[0] + t[1]
102-
_has_res = 1
103-
except ImportError:
104-
pass
105-
10686
class Profile:
10787
"""Profiler class.
10888
@@ -155,20 +135,8 @@ def __init__(self, timer=None, bias=None):
155135
self 6D47 .bias = bias # Materialize in local dict for lookup speed.
156136

157137
if not timer:
158-
if _has_res:
159-
self.timer = resgetrusage
160-
self.dispatcher = self.trace_dispatch
161-
self.get_time = _get_time_resource
162-
elif hasattr(time, 'clock'):
163-
self.timer = self.get_time = time.clock
164-
self.dispatcher = self.trace_dispatch_i
165-
elif hasattr(os, 'times'):
166-
self.timer = os.times
167-
self.dispatcher = self.trace_dispatch
168-
self.get_time = _get_time_times
169-
else:
170-
self.timer = self.get_time = time.time
171-
self.dispatcher = self.trace_dispatch_i
138+
self.timer = self.get_time = time.process_time
139+
self.dispatcher = self.trace_dispatch_i
172140
else:
173141
self.timer = timer
174142
t = self.timer() # test out timer function

Lib/test/reperf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def main():
99
timefunc(10, p.findall, s)
1010

1111
def timefunc(n, func, *args, **kw):
12-
t0 = time.clock()
12+
t0 = time.perf_counter()
1313
try:
1414
for i in range(n):
1515
result = func(*args, **kw)
1616
return result
1717
finally:
18-
t1 = time.clock()
18+
t1 = time.perf_counter()
1919
if n > 1:
2020
print(n, "times", end=' ')
2121
print(func.__name__, "%.3f" % (t1-t0), "CPU seconds")

Lib/test/sortperf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def flush():
5757
sys.stdout.flush()
5858

5959
def doit(L):
60-
t0 = time.clock()
60+
t0 = time.perf_counter()
6161
L.sort()
62-
t1 = time.clock()
62+
t1 = time.perf_counter()
6363
print("%6.2f" % (t1-t0), end=' ')
6464
flush()
6565

Lib/timeit.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
-n/--number N: how many times to execute 'statement' (default: see below)
1616
-r/--repeat N: how many times to repeat the timer (default 3)
1717
-s/--setup S: statement to be executed once initially (default 'pass')
18-
-t/--time: use time.time() (default on Unix)
19-
-c/--clock: use time.clock() (default on Windows)
18+
-t/--time: use time.time()
19+
-c/--clock: use time.clock()
2020
-v/--verbose: print raw timing results; repeat for more digits precision
2121
-h/--help: print this usage message and exit
2222
--: separate options from statement, use when statement starts with -
@@ -66,13 +66,7 @@
6666
dummy_src_name = "<timeit-src>"
6767
default_number = 1000000
6868
default_repeat = 3
69-
70-
if sys.platform == "win32":
71-
# On Windows, the best timer is time.clock()
72-
default_timer = time.clock
73-
else:
74-
# On most other platforms the best timer is time.time()
75-
default_timer = time.time
69+
default_timer = time.perf_counter
7670

7771
# Don't change the indentation of the template; the reindent() calls
7872
# in Timer.__init__() depend on setup being indented 4 spaces and stmt

Tools/pybench/pybench.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@
7373

7474
# Timer types
7575
TIMER_TIME_TIME = 'time.time'
76+
TIMER_TIME_PROCESS_TIME = 'time.process_time'
77+
TIMER_TIME_PERF_COUNTER = 'time.perf_counter'
7678
TIMER_TIME_CLOCK = 'time.clock'
7779
TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime'
7880

7981
# Choose platform default timer
80-
if sys.platform[:3] == 'win':
82+
if hasattr(time, 'perf_counter'):
83+
TIMER_PLATFORM_DEFAULT = TIMER_TIME_PERF_COUNTER
84+
elif sys.platform[:3] == 'win':
8185
# On WinXP this has 2.5ms resolution
8286
TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK
8387
else:
@@ -93,6 +97,10 @@ def get_timer(timertype):
9397

9498
if timertype == TIMER_TIME_TIME:
9599
return time.time
100+
elif timertype == TIMER_TIME_PROCESS_TIME:
101+
return time.process_time
102+
elif timertype == TIMER_TIME_PERF_COUNTER:
103+
return time.perf_counter
96104
elif timertype == TIMER_TIME_CLOCK:
97105
return time.clock
98106
elif timertype == TIMER_SYSTIMES_PROCESSTIME:
@@ -866,7 +874,18 @@ def main(self):
866874
print('* using timer: systimes.processtime (%s)' % \
867875
systimes.SYSTIMES_IMPLEMENTATION)
868876
else:
877+
# Check that the clock function does exist
878+
try:
879+
get_timer(timer)
880+
except TypeError:
881+
print("* Error: Unknown timer: %s" % timer)
882+
return
883+
869884
print('* using timer: %s' % timer)
885+
if hasattr(time, 'get_clock_info'):
886+
info = time.get_clock_info(timer[5:])
887+
print('* timer: resolution=%s, implementation=%s'
888+
% (info.resolution, info.implementation))
870889

871890
print()
872891

0 commit comments

Comments
 (0)
0