Skip to content

Instantly share code, notes, and snippets.

@zed
Created March 18, 2011 09:03
Show Gist options
  • Save zed/875795 to your computer and use it in GitHub Desktop.
Save zed/875795 to your computer and use it in GitHub Desktop.
Time performance: `a.prod()` vs. `reduce(lambda x,y:x*y, a)`
In [1]: import numpy as np
In [2]: a = np.r_[.1:4:40j]
In [3]: a
Out[3]:
array([ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1,
1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2,
2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3,
3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. ])
In [4]: timeit reduce(lambda x,y:x*y, a)
100000 loops, best of 3: 18.1 us per loop
In [5]: timeit a.prod()
1000000 loops, best of 3: 1.88 us per loop
In [6]: reduce(lambda x,y:x*y, a)
Out[6]: 81591528.324789688
In [7]: a.prod()
Out[7]: 81591528.324789688
In [8]: timeit np.prod(a)
100000 loops, best of 3: 2.4 us per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment