Skip to content

Instantly share code, notes, and snippets.

Created Dec 22, 2021
What would you like to do?
import sys
from math import inf
import numpy as np
from random import random
from time import perf_counter as clock
def timeit(f, x):
t1 = clock()
return clock()-t1
def f1(a):
a = np.where(a<0, 0, a)
def f2(a):
a[a<0] = 0
def f3(a):
np.putmask(a, a<0, 0)
def f4(a):
a = np.maximum(a, 0)
def f5(a):
a = a.clip(0)
z = np.random.rand(10**7)
res = []
for i in range(30):
z = np.random.rand(10**7)
z -= random()
x = (z>0).sum()
r = []
for f in (f1, f2, f3, f4, f5):
r.append(min(timeit(f, z.copy()) for i in range(3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment