Skip to content

Instantly share code, notes, and snippets.

@fkdosilovic
Created January 20, 2024 19:22
Show Gist options
  • Save fkdosilovic/feda0de656067d26f0da5925f90b8183 to your computer and use it in GitHub Desktop.
Save fkdosilovic/feda0de656067d26f0da5925f90b8183 to your computer and use it in GitHub Desktop.
Benchmark numpy and scipy decompositions
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)
@fkdosilovic
Copy link
Author

fkdosilovic commented Jan 20, 2024

Results on AMD Ryzen 5 5625U with 6 cores (12 threads):

Timings are reported in seconds. Each experiment was run 10 times.

method shape avg std min max
numpy.linalg.svd (10, 10) 0.00 0.00 0.00 0.00
(100, 100) 0.00 0.00 0.00 0.01
(500, 500) 0.05 0.01 0.04 0.07
(1000, 1000) 0.23 0.02 0.21 0.27
(5000, 1000) 4.07 0.15 3.90 4.39
scipy.linalg.svd (10, 10) 0.00 0.00 0.00 0.00
(100, 100) 0.00 0.00 0.00 0.01
(500, 500) 0.04 0.00 0.04 0.05
(1000, 1000) 0.20 0.01 0.19 0.23
(5000, 1000) 3.92 0.14 3.80 4.25

scipy v1.11.4 and numpy v1.26.3

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