[R] 6 times faster by eliminating apply

Jason Liao jg_liao at yahoo.com
Tue May 1 17:32:10 CEST 2001


This is some kind of follow-up to my previous posts. I have further
improved the speed of my program 6 times by eliminating all the
apply(). It turns out that apply is slow, is slower than direct loop,
it is an order slower than a matrix operation alternative.

Here is one example. The first apply version runs 19 seconds, the
second loop version runs 13 seconds, the third matrix version runs 1
second.

Jason Liao

 rm(list=ls(all=TRUE))

test1 <- function()
{
   u <- runif(n*m);
   dim(u) <- c(n,m);
   v <- apply(u, 2, crossprod);
   }
   
   
test2 <- function()
{   
   u <- runif(n*m);
   dim(u) <- c(n,m);
   v <- numeric(m);
   for(j in 1:m) v[j] <- crossprod(u[, j]);
   }
   
test3 <- function()
{
   u <- runif(n*m);
   dim(u) <- c(n,m);
   u <- u*u;
   one <- rep(1, n);
   v <- one %*% u;
   }
   
   
   n<-10;
   m<-10000;
  print(date());
  for(i in 1:10) test1()
 
   print(date());
   for(i in 1:10) test2()
 
   print(date());
   for(i in 1:10) test3()

   print(date());



=====
Jason G. Liao
Department of Biometry and Epidemiology
Medical University of South Carolina
135 Rutledge Ave., STE 1148, Charleston, SC 29425
phone (843) 876-1114, fax (843) 876-1126

http://www.geocities.com/jg_liao/index.html

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list