# [SciPy-user] More on speed comparisons

Matthieu Brucher matthieu.brucher@gmail....
Mon Jun 16 09:43:33 CDT 2008

```Hi,

Try with complete Numpy support :

# rand_test_2.py
from numpy import random, sin, sqrt, mean, var

N = 1000000
x = random.rand(N)
x = 3.14*sqrt(x)
x = sin(x)

mean_ = mean(x)
var_  = var(x)

print 'Mean=%g, var=%g' % (mean_, var_)

You can use numpy.sum as well.

Matthieu

2008/6/16 Ivo Maljevic <ivo.maljevic@gmail.com>:
> 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
>
>

--
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92