# [Numpy-discussion] numpy speed question

Dave Hirschfeld dave.hirschfeld@gmail....
Thu Nov 25 04:49:57 CST 2010

```Jean-Luc Menut <jeanluc.menut <at> free.fr> writes:
>
> I have a little question about the speed of numpy vs IDL 7.0.
>
> Here the IDL result:
> % Compiled module: \$MAIN\$.
>         2.9999837
>
> The python code:
> from numpy import *
> from time import time
> time1 = time()
> for j in range(10000):
>      for i in range(1000):
>          a=cos(2*pi*i/100.)
> time2 = time()
> print time2-time1
>
> result:
> In [2]: run python_test_speed.py
> 24.1809999943
>

Whilst you've imported everything from numpy you're not really using numpy -
you're still using a slow Python (double) loop. The power of numpy comes from
vectorising your code - i.e. applying functions to arrays of data.

The example below demonstrates an 80 fold increase in speed by vectorising the
calculation:

def method1():
a = empty([1000, 10000])
for j in range(10000):
for i in range(1000):
a[i,j] = cos(2*pi*i/100.)
return a
#

def method2():
ij = np.repeat((2*pi*np.arange(1000)/100.)[:,None], 10000, axis=1)
return np.cos(ij)
#

In [46]: timeit method1()
1 loops, best of 3: 47.9 s per loop

In [47]: timeit method2()
1 loops, best of 3: 589 ms per loop

In [48]: allclose(method1(), method2())
Out[48]: True

```