Skip to content

Instantly share code, notes, and snippets.

@p1-dta
Created March 7, 2025 15:52
Show Gist options
  • Save p1-dta/f86409dc4f77c40453691e4aca9aa261 to your computer and use it in GitHub Desktop.
Save p1-dta/f86409dc4f77c40453691e4aca9aa261 to your computer and use it in GitHub Desktop.
# requires-python = ">=3.13"
# dependencies = [
# "more-itertools>=10.6.0",
# "pytest>=8.3.5",
# "pytest-benchmark>=5.1.0",
# ]
import collections
import statistics
import more_itertools
import pytest
def performance(values: list[float], window_size: int) -> collections.abc.Generator[float]:
fifo = collections.deque()
values_iterator = iter(values)
sigma = 0
for _ in range(min(len(values), window_size)):
value = next(values_iterator)
sigma += value
fifo.append(value)
yield sigma / window_size
for value in values_iterator:
sigma += value
sigma -= fifo.popleft()
fifo.append(value)
yield sigma / window_size
def readability(values: list[float], window_size: int) -> collections.abc.Generator[float]:
for window in more_itertools.windowed(values, window_size):
yield statistics.mean(window)
@pytest.mark.parametrize(
"implementation",
[
performance,
readability,
]
)
def test_implementation(implementation):
assert (
list(implementation(range(5), 2))
== [0.5, 1.5, 2.5, 3.5]
)
@pytest.mark.parametrize(
("implementation"),
[
performance,
readability,
]
)
def test_benchmark_100(benchmark, implementation):
benchmark(lambda x, y: collections.deque(implementation(x, y), maxlen=0), range(100), 10)
@pytest.mark.parametrize(
("implementation"),
[
performance,
readability,
]
)
def test_benchmark_1000(benchmark, implementation):
benchmark(lambda x, y: collections.deque(implementation(x, y), maxlen=0), range(1_000), 100)
@pytest.mark.parametrize(
("implementation"),
[
performance,
readability,
]
)
def test_benchmark_10_000(benchmark, implementation):
benchmark(lambda x, y: collections.deque(implementation(x, y), maxlen=0), range(10_000), 1_000)
@p1-dta
Copy link
Author

p1-dta commented Mar 7, 2025

$ pytest --benchmark-group-by=func
============================================================================================================= test session starts =============================================================================================================
platform win32 -- Python 3.13.1, pytest-8.3.5, pluggy-1.5.0
benchmark: 5.1.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
configfile: pyproject.toml
plugins: benchmark-5.1.0
collected 8 items                                                                                                                                                                                                                              

test_moving_average.py ........                                                                                                                                                                                                          [100%]


------------------------------------------------------------------------------------ benchmark 'test_benchmark_100': 2 tests ------------------------------------------------------------------------------------
Name (time in us)                        Min                   Max                Mean              StdDev              Median                IQR            Outliers  OPS (Kops/s)            Rounds  Iterations
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_benchmark_100[performance]       9.4000 (1.0)         92.3000 (1.0)        9.9615 (1.0)        1.7513 (1.0)        9.7000 (1.0)       0.3000 (1.0)      579;2501      100.3863 (1.0)       39371           1
test_benchmark_100[readability]     526.4000 (56.00)    1,669.4000 (18.09)    584.2556 (58.65)    158.5218 (90.51)    545.3000 (56.22)    14.0750 (46.92)      81;218        1.7116 (0.02)       1693           1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------- benchmark 'test_benchmark_1000': 2 tests -------------------------------------------------------------------------------------------
Name (time in us)                            Min                    Max                   Mean                StdDev                 Median                   IQR            Outliers          OPS            Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_benchmark_1000[performance]         89.5000 (1.0)       1,618.4000 (1.0)          95.8373 (1.0)         22.8078 (1.0)          92.4000 (1.0)          3.0000 (1.0)       234;653  10,434.3540 (1.0)        8621           1
test_benchmark_1000[readability]     17,838.2000 (199.31)   37,599.2000 (23.23)    19,679.0500 (205.34)   3,191.1877 (139.92)   18,544.0000 (200.69)   1,792.7000 (597.57)        4;4      50.8155 (0.00)         56           1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------- benchmark 'test_benchmark_10_000': 2 tests ------------------------------------------------------------------------------------------------- 
Name (time in us)                                 Min                       Max                      Mean                  StdDev                    Median                     IQR            Outliers       OPS            Rounds  Iterations 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
test_benchmark_10_000[performance]           920.6000 (1.0)          5,500.0000 (1.0)          1,165.1538 (1.0)          290.2172 (1.0)          1,157.2000 (1.0)          212.8000 (1.0)         52;47  858.2558 (1.0)         995           1 
test_benchmark_10_000[readability]     1,444,352.6000 (>1000.0)  1,836,000.1000 (333.82)   1,712,798.8400 (>1000.0)  159,068.7660 (548.10)   1,732,930.3000 (>1000.0)  178,253.5250 (837.66)        1;0    0.5838 (0.00)          5           1 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean
============================================================================================================= 8 passed in 17.94s ============================================================================================================== 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment