perfplot extends Python's timeit by
testing snippets with input parameters (e.g., the size of an array) and plotting the
results.
For example, to compare different NumPy array concatenation methods, the script
import numpy as np
import perfplot
perfplot.show(
setup=lambda n: np.random.rand(n),
kernels=[
lambda a: np.c_[a, a],
lambda a: np.stack([a, a]).T,
lambda a: np.vstack([a, a]).T,
lambda a: np.column_stack([a, a]),
lambda a: np.concatenate([a[:, None], a[:, None]], axis=1),
],
labels=["c_", "stack", "vstack", "column_stack", "concat"],
n_range=[2**k for k in range(25)],
xlabel="len(a)",
)
produces
Clearly, stack
and vstack
are the best options for large arrays.
(By default, perfplot asserts the equality of the output of all snippets, too.)
If your plot takes a while to generate, you can also use
perfplot.live(
)
with the same arguments as above. It will plot the updates live.
Benchmarking and plotting can be separated. This allows multiple plots of the same data,
for example:
out = perfplot.bench(
)
out.show()
out.save("perf.png", transparent=True, bbox_inches="tight")
Other examples:
Installation
perfplot is available from the Python Package
Index, so simply do
pip install perfplot
to install.
Testing
To run the perfplot unit tests, check out this repository and type
tox
License
This software is published under the GPLv3 license.