Skip to content

Instantly share code, notes, and snippets.

@jaymody
Last active March 14, 2021 15:59
Show Gist options
  • Save jaymody/9d7dec07300f817ddd40b74b1d648a34 to your computer and use it in GitHub Desktop.
Save jaymody/9d7dec07300f817ddd40b74b1d648a34 to your computer and use it in GitHub Desktop.
NumPy operations profiling.
import timeit
import numpy as np
def broadcast():
a = np.random.random(256)
b = np.random.random((2048, 256))
return timeit.timeit(lambda: a - b, number=1000)
def tile():
a = np.random.random(256)
b = np.random.random((2048, 256))
n = b.shape[0]
return timeit.timeit(lambda: np.tile(a, (n, 1)) - b, number=1000)
# 0.3977937699999998
broadcast()
# 0.575944809000001
tile()
import time
import numpy as np
def profile(f, n=2000):
arr = np.random.random((1000, 100)) - 0.5
start = time.time()
for _ in range(n):
f(arr)
elapsed = time.time() - start
return elapsed
def maximum(a):
a = np.maximum(0, a)
return a
def indexing(a):
a[a > 0] = 0
return a
def where(a):
a = np.where(a > 0, a, 0)
return a
print(profile(maximum)) # 0.2757070064544678
print(profile(indexing)) # 0.06246805191040039
print(profile(where)) # 0.8790371417999268
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment