[R] R badly lags matlab on performance?

Ajay Shah ajayshah at mayin.org
Sat Jan 3 17:55:38 CET 2009


Here's a small R program:

---------------------------------------------------------------------------
a <- rep(1,10000000)

system.time(a <- a + 1)

system.time(for (i in 1:10000000) {a[i] <- a[i] + 1})
---------------------------------------------------------------------------

and here's its matlab version:

---------------------------------------------------------------------------
function forloop()

  a = ones(1e7,1);

  tic; a = a + 1; toc

  tic
  for i=1:1e7
    a(i) = a(i) + 1;
  end
  toc
---------------------------------------------------------------------------

The machine used for testing was an i386 core 2 duo machine at 2.2
GHz, running OS X `Tiger'. The R was 2.8.0 and the matlab was 2008a.

Here's the performance (time taken in seconds):

                        Matlab            R            Times
Vector version          0.0446            0.067        1.5x
For loop version        0.0992            42.209       425.5x

So the R is 1.5x costlier for the vector version and 425.5x costlier
with matlab.

I wonder what we're doing wrong!

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.




More information about the R-help mailing list