Created
January 28, 2016 12:11
-
-
Save d1manson/6b358e8f92b89ee76325 to your computer and use it in GitHub Desktop.
benchmark np basic math
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 matplotlib.pylab as plt | |
pow2s = arange(10,22,0.25) | |
t_times1 = np.zeros(len(pow2s)) | |
t_sum = np.zeros(len(pow2s)) | |
t_cumsum = np.zeros(len(pow2s)) | |
for ii, pow2 in enumerate(pow2s): | |
setup = "import numpy as np\na = np.random.rand({})".format(2**pow2) | |
t_times1[ii] = min(timeit.Timer('a*1.0', setup=setup).repeat(3, 100))/100 | |
t_sum[ii] = min(timeit.Timer('np.sum(a)', setup=setup).repeat(3, 100))/100 | |
t_cumsum[ii] = min(timeit.Timer('np.cumsum(a)', setup=setup).repeat(3, 100))/100 | |
plt.clf() | |
plt.plot(2**pow2s, t_times1, 'k') | |
plt.plot(2**pow2s, t_sum, 'b') | |
plt.plot(2**pow2s, t_cumsum, 'r') | |
plt.xlabel('len(a)') | |
plt.ylabel('time in s') | |
plt.gca().set_yscale('log') | |
plt.gca().set_xscale('log') | |
plt.legend(['times1','sum', 'cumsum'], loc=2) |
Author
d1manson
commented
Jan 28, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment