Skip to content

Instantly share code, notes, and snippets.

@axil
Created December 22, 2021 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axil/af6c4adb8c5634ff39ed9f3da1efaa90 to your computer and use it in GitHub Desktop.
Save axil/af6c4adb8c5634ff39ed9f3da1efaa90 to your computer and use it in GitHub Desktop.
positive
import numpy as np
from time import perf_counter as clock
z = np.random.rand(10**7) - 0.5
res = []
for i in range(30):
r = []
a = z.copy()
t1 = clock()
a = np.where(a<0, 0, a)
r.append(clock()-t1)
a = z.copy()
t1 = clock()
a[a<0] = 0
r.append(clock()-t1)
a = z.copy()
t1 = clock()
np.putmask(a, a<0, 0)
r.append(clock()-t1)
a = z.copy()
t1 = clock()
a = np.maximum(a, 0)
r.append(clock()-t1)
a = z.copy()
t1 = clock()
a = a.clip(0)
r.append(clock()-t1)
res.append(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment