Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I guess it might have been obvious, but I was surprised that rowSums
appeared much slower slower than directly doing the matrix operation. At first I assumed it was because it has some overhead in the form of input handling/casting and other odds and ends.
set.seed(1079711697)1 n <- 1e6 m <- 3 m1 <- matrix(rnorm(n*m), n, m) f1 <- function(m) m %*% cbind(rep(1, ncol(m))) f2 <- function(m) rowSums(m) microbenchmark::microbenchmark(f1(m1), f2(m1)) # Unit: milliseconds # expr min lq mean median uq max neval # f1(m1) 5.204801 5.335951 6.070637 5.433501 5.659101 20.2690 100 # f2(m1) 9.992201 10.181401 11.014871 10.254801 10.403851 31.1915 100
Above was run on a 6700K and R 4.3.1 (x86_64-w64-mingw32/x64). When I ran this on an Apple computer with an M1 though I was again surprised the pattern was reversed, but both were much faster and closer to one another– means were 3.3 and 2.7 for f1
and f2
respectively. That’s R 4.3.2 (aarch64-apple-darwin20). I haven’t messed with Rblas
, but maybe you can get additional benefits there?
It also looks like there are some slight precision differences between the two methods:
summary(f1(m1) - f2(m1)) # V1 # Min. :-1.776e-15 # 1st Qu.: 0.000e+00 # Median : 0.000e+00 # Mean :-1.190e-19 # 3rd Qu.: 0.000e+00 # Max. : 8.882e-16
-10-19 is pretty small. It happens to be about the charge of a single electron in SI units.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.