forked from WolframResearch/WolframClientForPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
60 lines (43 loc) · 1.32 KB
/
Copy pathdebug.py
File metadata and controls
60 lines (43 loc) · 1.32 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from decimal import Decimal
from functools import wraps
from wolframclient.utils.api import time
def timed(function):
def inner(*args, **opts):
t = time.perf_counter()
value = function(*args, **opts)
return time.perf_counter() - t, value
return inner
def echo(x):
print(x)
return x
def timed_repeated(N=30):
def repeated(function):
def inner(*args, **opts):
return repeated_timing(function, *args, N=N, **opts)
return inner
return repeated
def repeated_timing(function, *args, **opts):
N = opts.pop("N", 30)
timed_func = timed(function)
timers = []
for _ in range(N):
timer, res = timed_func(*args, **opts)
timers.append(timer)
timers = sorted(timers)
min = int(0.1 * N)
max = int(0.9 * N)
trimmed_timers = timers[min:max]
return sum(trimmed_timers) / len(trimmed_timers), res
def print_elapsed_time(viewfunc):
@wraps(viewfunc)
def inner(*args, **kw):
t = time.perf_counter()
res = viewfunc(*args, **kw)
print(
"Done %s: %s sec"
% (inner.__name__, Decimal(time.perf_counter() - t).quantize(Decimal("0.000000")))
)
return res
return inner