-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathtimer3.py
41 lines (31 loc) · 1.03 KB
/
timer3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File timer3.py
"""
Same usage as timer2.py, but uses 3.x keyword-only default arguments
istead of dict pops for simpler code. No need to hoist range() out of tests
in 3.x: always a generator in 3.x, and this can't run on 2.x
"""
import time
import sys
if sys.version_info[0] >= 3 and sys.version_info[1] >= 3:
timer = time.perf_counter
else:
timer = time.clock if sys.platform[:3] == 'win' else time.time
def total(func, *pargs, _reps=1000, **kargs):
repslist = list(range(_reps))
start = timer()
for i in repslist:
ret = func(*pargs, **kargs)
elapsed = timer() - start
return (elapsed, ret)
def bestof(func, *pargs, _reps=1000, **kargs):
repslist = list(range(_reps))
best = 2 ** 32
for i in repslist:
start = timer()
ret = func(*pargs, **kargs)
elapsed = timer() - start
if elapsed < best:
best = elapsed
return (best, ret)
def bestoftotal(func, *pargs, _reps1=5, **kargs):
return min(total(func, *pargs, **kargs) for i in range(_reps1))