Skip to content

Instantly share code, notes, and snippets.

@nitaruto
Created August 26, 2021 15:18
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 nitaruto/8c0d152f450a2f91934b99f3b0664603 to your computer and use it in GitHub Desktop.
Save nitaruto/8c0d152f450a2f91934b99f3b0664603 to your computer and use it in GitHub Desktop.
compare x ** p vs np.exp(p * np.log(x))
#!/usr/bin/env python3
import numpy as np
import timeit
def setup(N):
x = np.random.rand(N)
return x
def pow1(x, p):
return x ** p
def pow2(x, p):
return np.exp(p * np.log(x))
N = 10000
loop = 10000
p = 0.3
x = setup(N)
assert np.allclose(pow1(x, p), pow2(x, p))
result = timeit.timeit("y1 = pow1(x, p)", setup="x = setup(N)", globals=globals(), number=loop)
print(f"pow1(): {result}")
result = timeit.timeit("y2 = pow2(x, p)", setup="x = setup(N)", globals=globals(), number=loop)
print(f"pow2(): {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment