-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtest_performance.py
More file actions
55 lines (45 loc) · 1.65 KB
/
test_performance.py
File metadata and controls
55 lines (45 loc) · 1.65 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
import time
import timeit
import pytest
from uncertainties import ufloat
def repeated_summation(num):
"""
generate and sum many floats together, then calculate the standard deviation of the
output. Under the lazy expansion algorithm, the uncertainty remains non-expanded
until a request is made to calculate the standard deviation.
"""
result = sum(ufloat(1, 0.1) for _ in range(num)).std_dev
return result
def test_repeated_summation_complexity():
"""
Test that the execution time is linear in summation length
"""
approx_execution_time_per_n = 10e-6 # 10 us
target_test_duration = 1 # 1 s
n_list = [10, 100, 1000, 10000, 100000]
t_list = []
for n in n_list:
"""
Choose the number of repetitions so that the test takes target_test_duration
assuming the timing of a single run is approximately
N * approx_execution_time_per_n
"""
# Choose the number of repetitions so that the test
single_rep_duration = n * approx_execution_time_per_n
num_reps = int(target_test_duration / single_rep_duration)
t_tot = timeit.timeit(
lambda: repeated_summation(n),
number=num_reps,
timer=time.process_time,
)
t_single = t_tot / num_reps
t_list.append(t_single)
n0 = n_list[0]
t0 = t_list[0]
for n, t in zip(n_list[1:], t_list[1:]):
# Check that t vs n is sub-quadratic. See PR #275
assert t / t0 < (n / n0) ** 1.5
@pytest.mark.parametrize("num", (10, 100, 1000, 10000, 100000))
@pytest.mark.benchmark
def test_repeated_summation_speed(num):
repeated_summation(num)