Last active
November 5, 2021 08:56
-
-
Save nschloe/033c876ba9e573cb0d4de28bbb8e439f to your computer and use it in GitHub Desktop.
/-where() vs. divide()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import perfplot | |
import numpy as np | |
threshold = 0.5 | |
# threshold = 1.0e-3 | |
def div_where(data): | |
a, b = data | |
return a / np.where(b > threshold, b, 1.0) | |
def divide(data): | |
a, b = data | |
return np.divide(a, b, where=b > threshold, out=a) | |
b = perfplot.bench( | |
setup=lambda n: np.random.rand(2, n), | |
kernels=[div_where, divide], | |
n_range=[2 ** k for k in range(23)], | |
) | |
b.save("out.png") | |
b.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With threshold
0.5
: