[SciPy-user] More on speed comparisons

Bruce Southey bsouthey@gmail....
Mon Jun 16 10:41:26 CDT 2008


Ivo Maljevic wrote:
> I was planing to refrain from this, but I believe this might be an 
> interesting comparison for people who seriously think about switching 
> to SciPy/NumPy.
>
> Few days ago I wrote about speed comparison between the scalar and 
> vectorized versions of function calls. Based on several comments, I
> concluded that the same story that applies to Matlab and Octave 
> applies here: vectorize thy code, and speed gain will come.
>
> Before I show the results of vectorized vs. non-vectorized results, 
> just want to go on the record and say that I am by no means sayhing 
> that SciPy/NumPy is not good. I still like what has been done here. 
> There is a particular scenario that I use at my work where SciPy, 
> combined with matplotlib, is extremely useful. That scenario is the 
> following.
>
> In my wireless lab, I have basestations, mobile stations, whole bunch 
> of instruments and PCs either connected via network or GPIB cables 
> (for instruments).I use Python here to automate test cases and data 
> collection and the ability to do SSH and GPIB communication is very 
> useful. Once I collect data, I use SciPy for some simple 
> postprocessing and I generate PNG plots, and finally, I generate HTML 
> pages with results shown as tables and plots. So, it is all done in a 
> single language/script instead of having to break the processing into 
> several languages/scripts.
>
> However, I wanted to see if SciPy would be good enough speedwise to 
> completely replace Matlab. An, at least for the type of processing I 
> do, it comes nowhere near it. I wrote a small toy program that does 
> some simple random variable manipulation in several languages. The 
> python code consists of two versions, one uses for loop and basic 
> pathon libraries and the other uses nympy's vectorized form (there was 
> no difference between numpy and scipy).
>
> Here are the relative results after running the code on two machines:
>
> 64-bit Ubunty 8.04:
> =================
>
> Fortran    C       Octave    SciPy   Pure Python
> =================================================
> 1          1.2      2.2      16      20
>
>
> 32-Bit openSUSE 10.3:
> ==================
>
> Fortran    C       Octave    SciPy   Pure Python
> =================================================
> 1          1.2      2.4      15      19.4
>
>
> The numbers are rounded a little bit, but they are in that range. I 
> see two problems here:
>
> 1. SciPy is very slow, even when compared to Octave 3.0
> 2. It is only sligtly faster than Python with a for loop.
>
> Below is the source code for the two python versions. While this 
> processing is not from any real application,
> it is not very different from the processing I normally do.
>
> Now, it is very likely that for different type of processing people 
> will find SciPy fast enough (matrix inversions, eigenvalues, etc), but 
> for the type of
> processing I need it is not fast enough.
>
> Ivo
>
> ##########################################################################
> # rand_test_1.py
> from random import random
> from math import sqrt, sin
>
> N = 1000000
>
> mean = 0
> var  = 0
>
> for i in range(N):
>   x = random()
>   x = 3.14*sqrt(x)
>   x = sin(x)
>   mean += x
>   var += x**2
>
> mean = mean/N
> var  = var/N - mean**2
> print 'Mean=%g, var=%g' % (mean, var)
>
> # rand_test_2.py
> from numpy import random, sin, sqrt
>
> N = 1000000
>
> x = random.rand(N)
> x = 3.14*sqrt(x)
> x = sin(x)
>
> mean = sum(x)/N
> var  = sum(x**2)/N - mean**2
>
> print 'Mean=%g, var=%g' % (mean, var)
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>   
Hi,
Actually this was addressed in Hans Petter Langtangen's book 'Python 
scripting for Computational Science' and probably elsewhere before that. 
While the book is somewhat dated (2004) it does contain considerable 
useful information on numerical python. For example the section 4.2.3 
shows this problem of using scalar arguments in Numeric and numarray 
compared using  Python's math module.

Bruce


More information about the SciPy-user mailing list