File: test_bench_misc.py

package info (click to toggle)
pytest-codspeed 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,608 kB
  • sloc: ansic: 9,792; python: 2,066; sh: 41; makefile: 14
file content (72 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download
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(1000)) == 332833500