Skip to content

Instantly share code, notes, and snippets.

@axil
Created December 23, 2021 07:21
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/2241e62977f46753caac7005268d5b28 to your computer and use it in GitHub Desktop.
Save axil/2241e62977f46753caac7005268d5b28 to your computer and use it in GitHub Desktop.
positive array size
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(10, 30):
z = np.random.rand(2**i)
z -= 0.5
r = []
for f in (f1, f2, f3, f4, f5):
r.append(min(timeit(f, z.copy()) for i in range(3)))
res.append([2**i]+r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment