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  
ajays...@mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to