> On Nov 8, 2016, at 7:57 AM, Doran, Harold <hdo...@air.org> wrote: > > Without reaching out to another package in R, I wonder what the best way is > to speed enhance the following toy example? Over the years I have become very > comfortable with the family of apply functions and generally not good at > finding an improvement for speed. > > This toy example is small, but my real data has many millions of rows and the > same operations is repeated many times and so finding a less expensive > alternative would be helpful. > > mm <- matrix(rnorm(100), ncol = 10) > rn <- apply(mm, 1, prod)
I believe you will find that a for-loop is faster. library(microbenchmark) help(pac=microbenchmark) microbenchmark( forloop={ y = mm[,1]; for (i in 2:dim(mm)[2]) y=mm[,i]}, apply = apply(mm,2,prod) ) +---------- Unit: microseconds expr min lq mean median uq max neval cld forloop 10.735 11.6450 15.00425 13.1295 13.7455 115.600 100 a apply 60.775 63.2025 71.95027 64.4530 71.3525 209.309 100 b > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. David Winsemius Alameda, CA, USA ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.