Skip to content

Instantly share code, notes, and snippets.

@d1manson
Created January 28, 2016 12:11
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 d1manson/6b358e8f92b89ee76325 to your computer and use it in GitHub Desktop.
Save d1manson/6b358e8f92b89ee76325 to your computer and use it in GitHub Desktop.
benchmark np basic math
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)
@d1manson
Copy link
Author

np_bench

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment