Fwd: Why R is 200 times slower than Matlab ?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Why R is 200 times slower than Matlab ?

RKoenker
This is what I refer to as "ab uno disce omnes"-thinking:
-- from one example all is revealed -- but this is the antithesis
of statistical thinking.

Here is an example function, others can probably do better.
On a vector of length 300 it takes .018 seconds on my aging
G5 ppc mac:

pears <- function(x){
        n <- length(x)
        A <- combos(n,2)
        rbind(x[A[1,]],x[A[2,]])
        }

This is actually a (shameless) advertisement for the function
combos in my quantreg package which uses a slightly fancy
algorithm to order the n choose p indices as, say produced
by base R's combn() in such a way that adjacent columns
differ by only one element.

PS.  Isn't there a better way to do the indexing on the last line?


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


On Apr 30, 2008, at 3:15 PM, 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.
>
> Please help me out on this.
>
> 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
>
> --
> Zhandong Liu
>
> Genomics and Computational Biology
> University of Pennsylvania
>
> 616 BRB II/III, 421 Curie Boulevard
> University of Pennsylvania School of Medicine
> Philadelphia, PA 19104-6160
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.