-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
39 lines (35 loc) · 1.01 KB
/
timer.py
File metadata and controls
39 lines (35 loc) · 1.01 KB
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
'''
Homegrown timing tools for function calls.
Does total time, best-of time, and best-of-totals time
'''
import time, sys
timer = time.clock if sys.platform[:3] == 'win' else time.time
def total(reps, func, *pargs,**kargs):
'''
Total time to run func() reps times.
Return (total time, last result)
'''
repslist = list(range(reps))
start = timer()
for i in repslist:
ret = func(*pargs, **kargs)
elapsed = timer() - start
return (elapsed, ret)
def bestof(reps, func, *pargs, **kargs):
'''
Quickest func() among reps runs.
Return (best time, last result)
'''
bst = float('inf')
for i in range(reps):
start = timer()
ret = func(*pargs, **kargs)
elapsed = timer() - start
if elapsed < bst: bst = elapsed
return (bst, ret)
def best_of_total(reps1,reps2, func, *pargs,**kargs):
'''
Best of totals:
(best of reps1 runs of (total of reps2 runs of func))
'''
return bestof(reps1,total, reps2,func,*pargs,**kargs)