Created
August 26, 2021 15:18
-
-
Save nitaruto/8c0d152f450a2f91934b99f3b0664603 to your computer and use it in GitHub Desktop.
compare x ** p vs np.exp(p * np.log(x))
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
#!/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