Skip to content
Snippets Groups Projects
Commit d13b5b52 authored by Erik Frisk's avatar Erik Frisk
Browse files

polished plots

parent 3e794f18
No related branches found
No related tags found
No related merge requests found
# %%
import timeit import timeit
from my_ext import array_sum, array_sum_nocopy, array_sum_pyobj # noqa from my_ext import array_sum, array_sum_nocopy, array_sum_pyobj # noqa
import numpy as np import numpy as np
...@@ -5,25 +6,39 @@ import matplotlib.pyplot as plt ...@@ -5,25 +6,39 @@ import matplotlib.pyplot as plt
from seaborn import despine from seaborn import despine
# %% # %% Utils
M = 500 def bs_mean_ci(x, alpha=0.95, n_bootstrap=1000):
N = np.logspace(1, 5, 10).astype(int) """Bootstrap mean and confidence interval."""
n, _ = x.shape
mu = np.zeros(n)
ci = np.zeros((n, 2))
for k, xi in enumerate(x):
bs = np.random.choice(xi, size=(n_bootstrap,), replace=True)
mu[k] = bs.mean()
ci[k] = np.percentile(bs, [(1 - alpha) * 100 / 2, 100 - (1 - alpha) * 100 / 2])
return mu, ci
# %% Perform timing experiments
R = 20 # Number of repeated timing experiments
M = 500 # Number of iterations in each timing measurement
N = np.logspace(1, 5, 10).astype(int) # Sizes to test
res = {"np": [], "array_sum": [], "array_sum_nocopy": [], "array_sum_pyobj": []} res = {"np": [], "array_sum": [], "array_sum_nocopy": [], "array_sum_pyobj": []}
for Ni in N: for Ni in N:
print(f"Ni = {Ni}") print(f"Ni = {Ni}")
x = np.random.normal(0, 1, int(Ni)) x = np.random.normal(0, 1, int(Ni))
ri = timeit.repeat("x.sum()", globals=globals(), number=M, repeat=5) ri = np.array(timeit.repeat("x.sum()", globals=globals(), number=M, repeat=R)) / M
res["np"].append(np.median(ri) / M) res["np"].append(ri)
ri = timeit.repeat("array_sum(x)", globals=globals(), number=M, repeat=5) ri = np.array(timeit.repeat("array_sum(x)", globals=globals(), number=M, repeat=R)) / M
res["array_sum"].append(np.median(ri) / M) res["array_sum"].append(ri)
ri = timeit.repeat("array_sum_nocopy(x)", globals=globals(), number=M, repeat=5) ri = np.array(timeit.repeat("array_sum_nocopy(x)", globals=globals(), number=M, repeat=R)) / M
res["array_sum_nocopy"].append(np.median(ri) / M) res["array_sum_nocopy"].append(ri)
ri = timeit.repeat("array_sum_pyobj(x)", globals=globals(), number=M, repeat=5) ri = np.array(timeit.repeat("array_sum_pyobj(x)", globals=globals(), number=M, repeat=R)) / M
res["array_sum_pyobj"].append(np.median(ri) / M) res["array_sum_pyobj"].append(ri)
for k in res: for k in res:
res[k] = np.array(res[k]) res[k] = np.array(res[k])
...@@ -31,10 +46,10 @@ for k in res: ...@@ -31,10 +46,10 @@ for k in res:
# %% Plot results # %% Plot results
fig, ax = plt.subplots(num=10, clear=True) fig, ax = plt.subplots(num=10, clear=True)
ax.loglog(N, res["np"] * 1e9, label="np") for k in res:
ax.loglog(N, res["array_sum"] * 1e9, label="array_sum") mu, ci = bs_mean_ci(res[k])
ax.loglog(N, res["array_sum_nocopy"] * 1e9, label="array_sum_nocopy") ax.loglog(N, mu, label=k)
ax.loglog(N, res["array_sum_pyobj"] * 1e9, label="array_sum_pyobj") ax.fill_between(N, ci[:, 0], ci[:, 1], alpha=0.2)
ax.legend(frameon=False) ax.legend(frameon=False)
ax.set_xlabel("N") ax.set_xlabel("N")
ax.set_ylabel("Time (ns)") ax.set_ylabel("Time (ns)")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment