Skip to content

Instantly share code, notes, and snippets.

@axil
Created December 22, 2021 16:03
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/ce4ecdf1cb0446db47b979c37ed5fba3 to your computer and use it in GitHub Desktop.
Save axil/ce4ecdf1cb0446db47b979c37ed5fba3 to your computer and use it in GitHub Desktop.
positive
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()
f(x)
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)))
res.append([x]+r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment