Skip to content

Instantly share code, notes, and snippets.

@nitaruto
Created Aug 26, 2021
Embed
What would you like to do?
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