Created
January 1, 2023 15:07
-
-
Save ykm11/b5d0ebccb250fa68f94aff9698d64e0c to your computer and use it in GitHub Desktop.
Comparing numpy.power
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
import timeit | |
import numpy as np | |
def mypower(y): | |
y2 = y*y | |
y3 = y*y2 | |
y4 = y2*y2 | |
y5 = y2*y3 | |
y6 = y3*y3 | |
return y2, y3, y4, y5, y6 | |
def fff(array): | |
y2 = array**2 | |
y3 = array**3 | |
y4 = array**4 | |
y5 = array**5 | |
y6 = array**6 | |
return y2, y3, y4, y5, y6 | |
def ggg(array): | |
y2 = np.power(array, 2) | |
y3 = np.power(array, 3) | |
y4 = np.power(array, 4) | |
y5 = np.power(array, 5) | |
y6 = np.power(array, 6) | |
return y2, y3, y4, y5, y6 | |
N = 1000 | |
M = 300000 | |
np.random.seed(0) | |
a = np.random.random(M) | |
#y2_a, y3_a, y4_a, y5_a, y6_a = mypower(a) | |
#y2_b, y3_b, y4_b, y5_b, y6_b = fff(a) | |
#y2_c, y3_c, y4_c, y5_c, y6_c = ggg(a) | |
result = timeit.timeit('mypower(a)', globals=globals(), number=N) | |
print(result / N) | |
result = timeit.timeit('fff(a)', globals=globals(), number=N) | |
print(result / N) | |
result = timeit.timeit('ggg(a)', globals=globals(), number=N) | |
print(result / N) |
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
ykm11: $ python3 measure_power.py | |
0.0023443464230003884 | |
0.06853497792999952 | |
0.0704053728960007 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment