Skip to content

Instantly share code, notes, and snippets.

@samaid
Created May 14, 2023 19:55
Show Gist options
  • Save samaid/a8ca134d982a716dad9e59e55c79319b to your computer and use it in GitHub Desktop.
Save samaid/a8ca134d982a716dad9e59e55c79319b to your computer and use it in GitHub Desktop.
Power is only marginally slower in unnested loop
from numba import njit, prange
import numpy as np
from time import time
import math
@njit(parallel=False)
def foo(m):
pos = np.empty_like(m)
for i in prange(len(m)):
# pos[i] = m[i]
# 0.42354917526245117
# 0.0394282341003418
pos[i] = m[i] ** (-1.5)
# 0.5008852481842041
# 0.1034078598022461
return pos
N = 10000000
m = np.random.uniform(10, 100, N)
t1 = time()
pos = foo(m)
t2 = time()
print(t2-t1)
t1 = time()
pos = foo(m)
t2 = time()
print(t2-t1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment