# Re: [R] Why R is 200 times slower than Matlab ?

From: Ray Brownrigg <Ray.Brownrigg_at_mcs.vuw.ac.nz>
Date: Thu, 01 May 2008 10:03:12 +1200

Ah, so the code is quite similar in MATLAB (and the *algorithm* is the same :-) ).

The "Important programming tip" is that when converting from MATLAB to R, you shouldn't just 'translate' from MATLAB code to R code, you must reconsider the problem in the context of the R environment. This is very much like translating poetry, where the result should really be a poem in the target language, not just an accurate word-for-word (or even sentence-for-sentence) translation.

Ray

On Thu, 01 May 2008, you wrote:
> This is the missing Matlab code:
>
> function[fc_matrix]=grw_permute(fc_vector)
>
>
>
> n=length(fc_vector);
>
>
>
> fc_matrix=zeros(2,n^2);
>
>
>
> index=1;
>
> for i=1:n
>
> for j=1:n
>
> fc_matrix(index)=fc_vector(i);
>
> fc_matrix(index+1)=fc_vector(j);
>
> index=index+2;
>
> end
>
> end
>
>
>
>
>
>
> On Wed, Apr 30, 2008 at 4:47 PM, Ray Brownrigg
> <Ray.Brownrigg_at_mcs.vuw.ac.nz>
>
> wrote:
> > On Thu, 01 May 2008, Zhandong Liu wrote:
> > > I am switching from Matlab to R, but I found that R is 200 times slower
> > > than matlab.
> > >
> > > Since I am newbie to R, I must be missing some important programming
> >
> > tips.
> >
> > >
> > > Here is the function:
> > > ## make the full pair-wise permutation of a vector
> > > ## input_fc=c(1,2,3);
> > > ## output_fc=(
> > > 1 1 1 2 2 2 3 3 3
> > > 1 2 3 1 2 3 1 2 3
> > > );
> > >
> > > grw_permute = function(input_fc){
> > >
> > > fc_vector = input_fc
> > >
> > > index = 1
> > >
> > > k = length(fc_vector)
> > >
> > > fc_matrix = matrix(0,2,k^2)
> > >
> > > for(i in 1:k){
> > >
> > > for(j in 1:k){
> > >
> > > fc_matrix[index] = fc_vector[i]
> > >
> > > fc_matrix[index+1] = fc_vector[j]
> > >
> > > index = index+2
> > >
> > > }
> > >
> > > }
> > >
> > > return(fc_matrix)
> > >
> > > }
> > >
> > > For an input vector of size 300. It took R 2.17 seconds to run.
> > >
> > > But the same code in matlab only needs 0.01 seconds to run.
> >
> > I am not a MATLAB user, but I suspect it wasn't "the same code" that
> > produced
> > an answer in MATLAB, but you don't provide your MATLAB code, nor do you
> > specify what version of R, of MATLAB, or what hardware and OS you are
> > using.
> >
> > I get {NetBSD, R version 2.6.0 (2007-10-03), Core 2 Duo, 3.x GHz}:
> > > input_fc <- sample(1:600)
> > > unix.time(a1 <- grw_permute(input_fc))
> >
> > user system elapsed
> > 3.279 -0.001 3.280
> >
> > > unix.time({n <- length(input_fc); a2 <- matrix(c(rep(input_fc, each=n),
> >
> > rep(input_fc, n)), 2, n*n, byrow = T)})
> > user system elapsed
> > 0.019 0.020 0.040
> >
> > > all.equal(a1, a2)
> >
> > [1] TRUE
> >
> > A sample of length 300 took less than 1 second using your grw_permute()
> > (so
> > your OS may be making a difference as well).
> >
> > > Am I missing sth in R.. Is there a away to optimize. ???
> >
> > Yes. Loops are not efficient in R.
> >
> > > Thanks
> >
> > HTH,
> > Ray Brownrigg

R-help_at_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. Received on Wed 30 Apr 2008 - 22:06:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Apr 2008 - 23:30:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.