[Numpy-discussion] numpy speed question
Bruce Sherwood
basherwo@ncsu....
Fri Nov 26 10:48:39 CST 2010
Although this was mentioned earlier, it's worth emphasizing that if
you need to use functions such as cosine with scalar arguments, you
should use math.cos(), not numpy.cos(). The numpy versions of these
functions are optimized for handling array arguments and are much
slower than the math versions for scalar arguments.
Bruce Sherwood
On Thu, Nov 25, 2010 at 2:34 PM, Gökhan Sever <gokhansever@gmail.com> wrote:
> On Thu, Nov 25, 2010 at 4:13 AM, Jean-Luc Menut <jeanluc.menut@free.fr> wrote:
>> Hello all,
>>
>> I have a little question about the speed of numpy vs IDL 7.0. I did a
>> very simple little check by computing just a cosine in a loop. I was
>> quite surprised to see an order of magnitude of difference between numpy
>> and IDL, I would have thought that for such a basic function, the speed
>> would be approximatively the same.
>>
>> I suppose that some of the difference may come from the default data
>> type of 64bits in numpy and 32 bits in IDL. Is there a way to change the
>> numpy default data type (without recompiling) ?
>>
>> And I'm not an expert at all, maybe there is a better explanation, like
>> a better use of the several CPU core by IDL ?
>>
>> I'm working with windows 7 64 bits on a core i7.
>>
>> any hint is welcome.
>> Thanks.
>>
>> Here the IDL code :
>> Julian1 = SYSTIME( /JULIAN , /UTC )
>> for j=0,9999 do begin
>> for i=0,999 do begin
>> a=cos(2*!pi*i/100.)
>> endfor
>> endfor
>> Julian2 = SYSTIME( /JULIAN , /UTC )
>> print, (Julian2-Julian1)*86400.0
>> print,cpt
>> end
>>
>> 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
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
> Vectorised numpy version already blow away the results.
>
> Here is what I get using the IDL version (with IDL v7.1):
>
> IDL> .r test_idl
> % Compiled module: $MAIN$.
> 4.0000185
>
> I[10]: time run test_python
> 43.305727005
>
> and using a Cythonized version:
>
> from math import pi
>
> cdef extern from "math.h":
> float cos(float)
>
> cpdef float myloop(int n1, int n2, float n3):
> cdef float a
> cdef int i, j
> for j in range(n1):
> for i in range(n2):
> a=cos(2*pi*i/n3)
>
> compiling the setup.py file python setup.py build_ext --inplace
> and importing the function into IPython
>
> from mycython import myloop
>
> I[6]: timeit myloop(10000, 1000, 100.0)
> 1 loops, best of 3: 2.91 s per loop
>
>
> --
> Gökhan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list