Skip to content

Instantly share code, notes, and snippets.

@nschloe
Last active November 5, 2021 08:56
Show Gist options
  • Save nschloe/033c876ba9e573cb0d4de28bbb8e439f to your computer and use it in GitHub Desktop.
Save nschloe/033c876ba9e573cb0d4de28bbb8e439f to your computer and use it in GitHub Desktop.
/-where() vs. divide()
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()
@nschloe
Copy link
Author

nschloe commented Nov 5, 2021

With threshold 0.5:

out

@nschloe
Copy link
Author

nschloe commented Nov 5, 2021

With threshould 1.0e-3:

out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment