[SciPy-user] NumPy vs. SciPy and other speed comparisons

Ivo Maljevic ivo.maljevic@gmail....
Tue Jun 10 19:49:59 CDT 2008


Hi,
I am relatively new SciPy/NumPy user and I must say I like it. I don't know
if this is something that is well known, but I run some simple tests,
and I found out that, depending on whether one imports NumPy or SciPy,
programs executes at quite different speeds. I'm guessing the problem has
something to do with how it cycles through loops.

My initial goal was to compare python scripts with octave/matlab ones, but
then I noticed the speed difference between NumPy and SciPy.

To cut the story short, here are results:

Execution times in seconds, test 1:

Python              Octave       Fortran            C
========================================================
NumPy: 23.9         36.05        6.7                6.8
SciPy: 44.5


Execution times in seconds, test 2:

Python              Octave       Fortran            C
========================================================
NumPy: 9.4          5.5          6.7                6.8
SciPy: 10.1


Test cases 1 and 2 refer to different parameter values for the same scipts
(test 1: FrameSize=100, test 2: FrameSize=10000).

If anybody would like to experiment, I've copied the source code for the
simple BPSK simulation that determines the raw bit error rate.

To calculate erfc function with NumPy, I used a simple fotran funciton and
comiled it into .so library:

subroutine erfc(x,y)
  real, intent(in)  :: x
  real, intent(out) :: y
  y = 1-erf(x)
  end

# Python script

# For some reason, numpy is much faster than scipy, Try to comment out one
or the other line
#from numpy import *
from scipy import *
import erfc

SNR_MIN   = -1
SNR_MAX   = 10
FrameSize = 100   # <-- change me for test 2

Eb_No_dB  = arange(SNR_MIN,SNR_MAX+1)

# signal vector
s = ones(FrameSize)
a = random.rand(FrameSize)
a_i = where(a < 0.5)
s[a_i] = -1

for snr in Eb_No_dB:

  No        = 10**(-snr/10.0)
  Pe        = 0.5*erfc.erfc(sqrt(1.0/No))
  nFrames   = ceil(200.0/FrameSize/Pe)
  error_sum = 0

  for frame in arange(nFrames):

    # noise
    #n = sqrt(No/2)*random.randn(FrameSize)
    n = random.normal(scale=sqrt(No/2), size=FrameSize)
    x = s + n

    # detection
    y = sign(x)

    # error counting
    err = where (y != s)
    error_sum += len(err[0])

  print 'Eb_No_dB=%2d, BER=%10.4e, Pe=%10.4e' % \
         (snr, error_sum/(FrameSize*nFrames), Pe)


Octave m-file:
% bpsk_sim.m - octave version
clear

tic

SNR_MIN    = -1;
SNR_MAX    = 10;
FrameSize  = 100;
Eb_No_dB = SNR_MIN:SNR_MAX;

% signal vector
s = ones(1,FrameSize);
a = rand(1,FrameSize);
a_i = find(a < 0.5);
s(a_i) = -1;

for snr=SNR_MIN:SNR_MAX

  No = 10^(-snr/10.0);
  Pe = 0.5*erfc(sqrt(1.0/No));
  nFrames = ceil(200/FrameSize/Pe);
  error_sum = 0;

  for frame=1:nFrames

    % noise
    n = sqrt(No/2)*randn(1,FrameSize);
    x = s + n;

    y = sign(x);

    err = find( y ~= s);
    error_sum = error_sum + length(err);
  end

  fprintf('Eb_No_dB=%2d, BER=%10.4e, Pe=%10.4e\n', ...
           snr, error_sum/(FrameSize*nFrames), Pe)
end
toc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080610/062c0d86/attachment.html 


More information about the SciPy-user mailing list