Skip to content

Commit 7725039

Browse files
Update benchmark script
- Use `microbenchmark` instead of `rbenchmark` Report relative and absolute timings - Use `fastLmPure` functions instead of calling C++ directly
1 parent 1a077fa commit 7725039

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

ChangeLog

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
2018-05-30 Michael Weylandt <michael.weylandt@gmail.com>
22

3-
* inst/examples/lmBenchmark.R: Update call to RcppArmadillo's version
4-
of fastLm in benchmark script
3+
* inst/examples/lmBenchmark.R: Update benchmark script to use
4+
microbenchmark and to use exposed fastLm functions from Rcpp
5+
packages rather than invoking .Call directly
56

67
2018-05-25 Ralf Stubner <ralf.stubner@daqana.com>
78

inst/examples/lmBenchmark.R

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## This file is part of RcppEigen.
66

77
require("stats", character=TRUE, quietly=TRUE)
8-
require("rbenchmark", character=TRUE, quietly=TRUE)
8+
require("microbenchmark", character=TRUE, quietly=TRUE)
99
require("RcppEigen", character=TRUE, quietly=TRUE)
1010

1111
## define different versions of lm
@@ -15,45 +15,61 @@ exprs <- list()
1515
## handle rank-deficient cases.
1616

1717
# default version used in lm()
18-
exprs$lm.fit <- expression(stats::lm.fit(mm, y))
18+
exprs["lm.fit"] <- alist(stats::lm.fit(mm, y))
1919
# versions from RcppEigen
2020
## column-pivoted QR decomposition - similar to lm.fit
21-
exprs$PivQR <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 0L, PACKAGE="RcppEigen"))
21+
exprs["PivQR"] <- alist(RcppEigen::fastLmPure(mm, y, 0L))
2222
## LDLt Cholesky decomposition with rank detection
23-
exprs$LDLt <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 2L, PACKAGE="RcppEigen"))
23+
exprs["LDLt"] <- alist(RcppEigen::fastLmPure(mm, y, 2L))
2424
## SVD using the Lapack subroutine dgesdd and Eigen support
25-
exprs$GESDD <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 6L, PACKAGE="RcppEigen"))
25+
exprs["GESDD"] <- alist(RcppEigen::fastLmPure(mm, y, 6L))
2626
## SVD (the JacobiSVD class from Eigen)
27-
exprs$SVD <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 4L, PACKAGE="RcppEigen"))
27+
exprs["SVD"] <- alist(RcppEigen::fastLmPure(mm, y, 4L))
2828
## eigenvalues and eigenvectors of X'X
29-
exprs$SymmEig <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 5L, PACKAGE="RcppEigen"))
29+
exprs["SymmEig"] <- alist(RcppEigen::fastLmPure(mm, y, 5L))
3030

3131
## Non-rank-revealing decompositions. These work fine except when
3232
## they don't.
3333

3434
## Unpivoted QR decomposition
35-
exprs$QR <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 1L, PACKAGE="RcppEigen"))
35+
exprs["QR"] <- alist(RcppEigen::fastLmPure(mm, y, 1L))
3636
## LLt Cholesky decomposition
37-
exprs$LLt <- expression(.Call("RcppEigen_fastLm_Impl", mm, y, 3L, PACKAGE="RcppEigen"))
37+
exprs["LLt"] <- alist(RcppEigen::fastLmPure(mm, y, 3L))
3838

3939
if (suppressMessages(require("RcppArmadillo", character=TRUE, quietly=TRUE))) {
40-
exprs$arma <- expression(.Call("_RcppArmadillo_fastLm_impl", mm, y, PACKAGE="RcppArmadillo"))
40+
exprs["arma"] <- alist(RcppArmadillo::fastLmPure(mm, y))
4141
}
4242

4343
if (suppressMessages(require("RcppGSL", character=TRUE, quietly=TRUE))) {
44-
exprs$GSL <- expression(.Call("RcppGSL_fastLm", mm, y, PACKAGE="RcppGSL"))
44+
exprs["GSL"] <- alist(RcppGSL::fastLmPure(mm, y))
4545
}
4646

4747
do_bench <- function(n=100000L, p=40L, nrep=20L, suppressSVD=(n > 100000L)) {
4848
mm <- cbind(1, matrix(rnorm(n * (p - 1L)), nc=p-1L))
4949
y <- rnorm(n)
5050
if (suppressSVD) exprs <- exprs[!names(exprs) %in% c("SVD", "GSL")]
5151
cat("lm benchmark for n = ", n, " and p = ", p, ": nrep = ", nrep, "\n", sep='')
52-
do.call(benchmark, c(exprs,
53-
list(order="relative",
54-
columns = c("test", "relative",
55-
"elapsed", "user.self", "sys.self"),
56-
replications = nrep)))
52+
mb <- microbenchmark(list=exprs, times = nrep)
53+
54+
op <- options(microbenchmark.unit="relative")
55+
on.exit(options(op))
56+
57+
mb_relative <- summary(mb)
58+
levels(mb_relative$expr) <- names(exprs)
59+
60+
options(microbenchmark.unit=NULL)
61+
mb_absolute <- summary(mb)
62+
levels(mb_absolute$expr) <- names(exprs)
63+
64+
mb_combined <- merge(mb_relative[, c("expr", "median")],
65+
mb_absolute[, c("expr", "median")],
66+
by="expr")
67+
68+
colnames(mb_combined) <- c("Method",
69+
"Relative",
70+
paste0("Elapsed (", attr(mb_absolute, "unit"), ")"))
71+
72+
mb_combined[order(mb_combined$Relative),]
5773
}
5874

5975
print(do_bench())

0 commit comments

Comments
 (0)