Search code examples
pythonperformanceperfplot

Accessing data that does not pass equality check on perfplot


I'm running script that compares performance of two different functions f1 and f2. It can't pass an equality_check and raises AssertionError: Equality check failure. (f1, f2). How can I access argument and outputs of functions where it fails?

perfplot.show(
    setup=lambda n: np.random.randint(0, n, size = n),
    kernels=[f1, f2],
    n_range=[2 ** k for k in range(3, 14)],
    logx=True,
    logy=True,
    xlabel="n",
    equality_check=lambda x, y: np.array_equal(x, y)
)

Solution

  • In each step, the argument is passed to the functions f1 and f2 by perfplot, and the outputs of both functions are passed to the function specified in the parameter equality_check of perfplot.show (or perfplot.bench) function:

    equality_check=foo  # foo(x, y)
    

    In the code you provided, you defined that parameter with an anonymous function (lambda):

    equality_check=lambda x, y: np.array_equal(x, y)
    

    Check if the two functions are indeed producing the same result (for the same input), and if that result is a NumPy array with the same shape, since you are using np.array_equal(x, y).


    If you still want to inspect those values during the process (but I recommend you check the functions f1 and f2 first), you can define the equality_check function as follows:

    def foo(x, y):
        equal = np.array_equal(x, y)
        if not equal:
            print(x, y)
        return equal
    

    That way, the outputs of f1 and f2 that cause the exception will be printed.