[SciPy-User] why is my scipy slow?

Pierre Barbier de Reuille pierre at barbierdereuille.net
Sat Feb 2 07:58:49 EST 2013


In this example, 99% of the time is spent in the convolution operation. But
you are comparing the generic nD convolution operation in python, against
the specialized 1D convolution in Matlab. Using the 1D convolution of scipy
(i.e. scipy.convolve), I get almost a factor 10 in speed on my machine.

In general, I find that Matlab can be a bit (~10%) faster on pure matrix
operations. However, the language is so much slower than python that any
useful code ends up being quite a lot faster in python. The worst case is
when dealing with irregular structures where cell-arrays would be used in
Matlab and dictionnaries in python. The only times Matlab can still end up
being faster, is if the JIT triggers and optimise the code correctly. In
these cases, you might want to use Cython, which can improve your speed a
lot sometimes just by compiling the python itself.

Hope this helps,

-- 
Barbier de Reuille Pierre


On 1 February 2013 04:55, Warren Weckesser <warren.weckesser at gmail.com>wrote:

>
>
> On Thu, Jan 31, 2013 at 10:39 PM, John <jmjatkins at gmail.com> wrote:
>
>> Hello,
>>
>> I've been using scipy for a few weeks now and for the most part am
>> thoughily
>> enjoying it! However I have been porting code from matlab and have been
>> surprissed by how much slower it is runnning under python. So much so
>> that I
>> suspect I must be doing something wrong. Below is an example. In matlab
>> the
>> doSomething() function takes 6.4ms. In python it taks 78ms, more than 10x
>> slower. Does this seem right? Or am I missing something? I installed the
>> Enthough distribution for Windows. Any advise much appreaciated!
>>
>> First in python:
>>
>> import time
>> import scipy.signal
>>
>> def speedTest():
>>     rep = 1000
>>     tt = time.time()
>>     for i in range(rep):
>>         doSomething()
>>     print (time.time() - tt) / rep
>>
>> def doSomething():
>>     lp = scipy.signal.firwin(16, 0.5);
>>     data = scipy.rand(100000)
>>     data = scipy.signal.convolve(data, lp)
>>
>> if __name__ == '__main__':
>>     speedTest()
>>
>>
>>
>> Now in matlab:
>>
>> function matlabSpeedTest()
>>     rep = 1000;
>>     tStart=tic;
>>     for j=1:rep
>>         doSomething();
>>     end
>>     tElapsed=toc(tStart)/rep;
>>     str = sprintf('time %s', tElapsed);
>>     disp(str);
>> end
>>
>> function data = doSomething()
>>     lp = fir1(16,0.5);
>>     data = rand(100000, 1, 'double');
>>     data = conv(lp, data);
>> end
>>
>>
>>
> There are several methods you can use to apply a FIR filter to a signal;
> scipy.signal.convolve is actually one of the slowest.  See
> http://www.scipy.org/Cookbook/ApplyFIRFilter for a comparison of the
> methods.
>
> Warren
>
>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130202/09b4e6e0/attachment.html>


More information about the SciPy-User mailing list