[Numpy-discussion] numpy speed question
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.
On Thu, Nov 25, 2010 at 2:34 PM, Gökhan Sever <email@example.com> wrote:
> On Thu, Nov 25, 2010 at 4:13 AM, Jean-Luc Menut <firstname.lastname@example.org> 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.
>> Here the IDL code :
>> Julian1 = SYSTIME( /JULIAN , /UTC )
>> for j=0,9999 do begin
>> for i=0,999 do begin
>> Julian2 = SYSTIME( /JULIAN , /UTC )
>> print, (Julian2-Julian1)*86400.0
>> % Compiled module: $MAIN$.
>> The python code:
>> from numpy import *
>> from time import time
>> time1 = time()
>> for j in range(10000):
>> for i in range(1000):
>> time2 = time()
>> print time2-time1
>> In : run python_test_speed.py
>> NumPy-Discussion mailing list
> 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$.
> I: time run test_python
> 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):
> compiling the setup.py file python setup.py build_ext --inplace
> and importing the function into IPython
> from mycython import myloop
> I: timeit myloop(10000, 1000, 100.0)
> 1 loops, best of 3: 2.91 s per loop
> NumPy-Discussion mailing list
More information about the NumPy-Discussion