Created
December 22, 2021 16:03
-
-
Save axil/ce4ecdf1cb0446db47b979c37ed5fba3 to your computer and use it in GitHub Desktop.
positive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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