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]]
Hi, ZD, Your comment about speed is too general. Here is a benchmark comparison among several languages and HTH. http://www.sciviews.org/benchmark/index.html On Wed, Apr 30, 2008 at 4:15 PM, Zhandong Liu <zhandong at mail.med.upenn.edu> 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]] > > ______________________________________________ > 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. >-- ==============================WenSui Liu ChoicePoint Precision Marketing Phone: 678-893-9457 Email : wensui.liu at choicepoint.com Blog : statcompute.spaces.live.com
I would rather not comment on matlab (where is your matlab code by the way?), but your function could be simplified a bit: grw.permute <- function(v) { cbind( rep(v, each=length(v)), rep(v, length(v)) ) }> system.time(tmp <- f( 1:300))user system elapsed 0.020 0.000 0.019 This is on my quite busy 4 years old laptop.... Best, Gabor On Wed, Apr 30, 2008 at 04:15:46PM -0400, 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. >[...] -- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
But please consider that this benchmark is five years old, and i believe that R has changed quite a lot since version 1.9. Gabor On Wed, Apr 30, 2008 at 04:21:51PM -0400, Wensui Liu wrote:> Hi, ZD, > Your comment about speed is too general. Here is a benchmark > comparison among several languages and HTH. > http://www.sciviews.org/benchmark/index.html >[...] -- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
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 > >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
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. > > 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.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.> ThanksHTH, Ray Brownrigg
You just have to use the right functions: is this fast enough> system.time(x <- expand.grid(1:300, 1:300))user system elapsed 0.00 0.01 0.01 On Wed, Apr 30, 2008 at 4:15 PM, Zhandong Liu <zhandong at mail.med.upenn.edu> 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]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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.The most important tip I would give you is to use the vectorized nature of R whenever possible. This helps avoid messy indexing and 'for' loops. Look at the following 3 functions. Yours, Gabor's, and my own (which I was about to post when I saw Gabor's nice solution, and is basically the same). Also see the system timings after the definitions. 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) } grw.permute2 <- function(v) { cbind( rep(v, each=length(v)), rep(v, length(v)) ) } grw_permute3 <- function(input_fc) { matrix(c(rep(input_fc, each = length(input_fc)), rep.int(input_fc, times = length(input_fc))), nrow = 2, byrow = TRUE) } > system.time(p1 <- grw_permute(1:300)) user system elapsed 1.548 0.064 2.341 > system.time(p2 <- grw_permute2(1:300)) user system elapsed 0.009 0.001 0.010 > system.time(p3 <- grw_permute3(1:300)) user system elapsed 0.008 0.002 0.010 Erik Iverson
Aside from optiming your code by making use of R functions that use C underneath as much as possible the big difference between R and Matlab is Matlab's just-in-time compilation of code. When that was introduced in Matlab huge speedups of Matlab programs were noticeable. For R, there is a new package on CRAN, jit, that aims to provide similar speedups. On Wed, Apr 30, 2008 at 4:15 PM, Zhandong Liu <zhandong at mail.med.upenn.edu> 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]] > > ______________________________________________ > 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. >