Created
January 20, 2024 19:22
-
-
Save fkdosilovic/feda0de656067d26f0da5925f90b8183 to your computer and use it in GitHub Desktop.
Benchmark numpy and scipy decompositions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import scipy as sp | |
import numpy as np | |
def benchmark(decomp, shapes, *, n_experiments=10): | |
def get_fn_name(fn): | |
return f"{fn.__module__}.{fn.__name__}" | |
print( | |
f"{'method':^14} {'shape':>16} " f"{'avg':^8} {'std':^8} {'min':^8} {'max':^8}" | |
) | |
for i, shape in enumerate(shapes): | |
mat = np.random.random(size=shape) | |
timings = [] | |
for _ in range(n_experiments): | |
start_time = time.perf_counter() | |
_ = decomp(mat) | |
end_time = time.perf_counter() | |
timings.append(end_time - start_time) | |
avg, std = np.mean(timings), np.std(timings) | |
min_, max_ = np.min(timings), np.max(timings) | |
fn_name = f"{decomp.__module__}.{decomp.__name__}" | |
print( | |
f"{fn_name if i == 0 else ' ':^14} {str(shape):>16} " | |
f"{avg:^8.2f} {std:^8.2f} {min_:^8.2f} {max_:^8.2f}" | |
) | |
shapes = [ | |
(10, 10), | |
(100, 100), | |
(500, 500), | |
(1000, 1000), | |
(5000, 1000), | |
] | |
benchmark(np.linalg.svd, shapes) | |
benchmark(sp.linalg.svd, shapes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on AMD Ryzen 5 5625U with 6 cores (12 threads):
Timings are reported in seconds. Each experiment was run 10 times.
scipy v1.11.4 and numpy v1.26.3