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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Apr 30 22:42:15 CEST 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.
>
> Am I missing sth in R.. Is there a away to optimize.  ???
>
> Thanks
>
>
This is pretty characteristic. With R, you really don't want nested
loops doing single-element accessing (if you have better things to do
with 2.16 seconds of our life). You will usually find that this sort of
problem is handled either using vectorized operations at a higher level,
or pushed into C code which is dynamically loaded. For the particular
problem, notice that the same result is obtained with

> system.time(rbind(rep(1:300,300),rep(1:300,each=300)))
user  system elapsed
0.041   0.006   0.050

or even (OK, so it's transposed)

> system.time(expand.grid(1:300,1:300))
user  system elapsed
0.027   0.011   0.040

--
O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907

```