Skip to content

paxtonfitzpatrick/perfplot

 
 

Repository files navigation

perfplot

PyPi Version PyPI pyversions GitHub stars PyPi downloads

gh-actions codecov LGTM Code style: black

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
import perfplot

perfplot.show(
    setup=lambda n: numpy.random.rand(n),  # or setup=numpy.random.rand
    kernels=[
        lambda a: numpy.c_[a, a],
        lambda a: numpy.stack([a, a]).T,
        lambda a: numpy.vstack([a, a]).T,
        lambda a: numpy.column_stack([a, a]),
        lambda a: numpy.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)",
    # logx=False,
    # logy=False,
    # More optional arguments with their default values:
    # logx="auto",  # set to True or False to force scaling
    # logy="auto",
    # equality_check=numpy.allclose,  # set to None to disable "correctness" assertion
    # show_progress=True,
    # target_time_per_measurement=1.0,
    # time_unit="s",  # set to one of ("auto", "s", "ms", "us", or "ns") to force plot units
    # relative_to=1,  # plot the timings relative to one of the measurements
    # flops=lambda n: 3*n,  # FLOPS plots
)

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.)

Benchmarking and plotting can be separated. This allows multiple plots of the same data, for example:

out = perfplot.bench(
    # same arguments as above (except the plot-related ones, like time_unit or log*)
    )
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

pytest

License

This software is published under the GPLv3 license.

About

Performance plots for Python code

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 95.0%
  • Makefile 5.0%