-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_bench_misc.py
72 lines (52 loc) · 1.54 KB
/
test_bench_misc.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pytest
def count_even_fast(arr):
"""Count the number of even numbers in an array."""
even = 0
for x in arr:
if x % 2 == 0:
even += 1
return even
def count_even_slow(arr):
"""Count the number of even numbers in an array."""
return sum(1 for x in arr if x % 2 == 0)
@pytest.mark.parametrize(
"func",
[
count_even_fast,
count_even_slow,
],
)
def test_count_even(func, benchmark):
assert benchmark(func, range(10_000)) == 5000
def sum_of_squares_for_loop_product(arr) -> int:
total = 0
for x in arr:
total += x * x
return total
def sum_of_squares_for_loop_power(arr) -> int:
total = 0
for x in arr:
total += x**2
return total
def sum_of_squares_sum_labmda_product(arr) -> int:
return sum(map(lambda x: x * x, arr)) # noqa: C417
def sum_of_squares_sum_labmda_power(arr) -> int:
return sum(map(lambda x: x**2, arr)) # noqa: C417
def sum_of_squares_sum_comprehension_product(arr) -> int:
return sum(x * x for x in arr)
def sum_of_squares_sum_comprehension_power(arr) -> int:
return sum(x**2 for x in arr)
@pytest.mark.parametrize(
"func",
[
sum_of_squares_for_loop_product,
sum_of_squares_for_loop_power,
sum_of_squares_sum_labmda_product,
sum_of_squares_sum_labmda_power,
sum_of_squares_sum_comprehension_product,
sum_of_squares_sum_comprehension_power,
],
)
@pytest.mark.benchmark
def test_sum_of_squares(func):
assert func(range(2000)) == 333328333350000