Skip to content

Instantly share code, notes, and snippets.

@axil
Created Dec 22, 2021
Embed
What would you like to do?
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