Skip to content

Commit 7a18153

Browse files
Condition benchmarking on microbenchmark package
- Move benchmark script into a `if(require("microbenchmark"))` block so script works even if microbenchmark is not installed - Add microbenchmark to `Suggests` - Add r-cran-microbenchmark to .travis.yml
1 parent 7725039 commit 7a18153

File tree

3 files changed

+70
-70
lines changed

3 files changed

+70
-70
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ before_install:
1313
- ./run.sh bootstrap
1414

1515
install:
16-
- ./run.sh install_aptget r-cran-rcpp r-cran-matrix r-cran-inline r-cran-runit r-cran-pkgkitten
16+
- ./run.sh install_aptget r-cran-rcpp r-cran-matrix r-cran-inline r-cran-runit r-cran-pkgkitten r-cran-microbenchmark
1717

18-
script:
18+
script:
1919
- ./run.sh run_tests
2020

2121
#after_success:
@@ -28,4 +28,3 @@ notifications:
2828
email:
2929
on_success: change
3030
on_failure: change
31-

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ Depends: R (>= 2.15.1)
2525
LazyLoad: yes
2626
LinkingTo: Rcpp
2727
Imports: Matrix (>= 1.1-0), Rcpp (>= 0.11.0), stats, utils
28-
Suggests: inline, RUnit, pkgKitten
28+
Suggests: inline, RUnit, pkgKitten, microbenchmark
2929
URL: http://dirk.eddelbuettel.com/code/rcpp.eigen.html
3030
BugReports: https://github.com/RcppCore/RcppEigen/issues

inst/examples/lmBenchmark.R

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

77
require("stats", character=TRUE, quietly=TRUE)
8-
require("microbenchmark", character=TRUE, quietly=TRUE)
98
require("RcppEigen", character=TRUE, quietly=TRUE)
109

11-
## define different versions of lm
12-
exprs <- list()
13-
14-
## These versions use rank-revealing decompositions and thus can
15-
## handle rank-deficient cases.
16-
17-
# default version used in lm()
18-
exprs["lm.fit"] <- alist(stats::lm.fit(mm, y))
19-
# versions from RcppEigen
20-
## column-pivoted QR decomposition - similar to lm.fit
21-
exprs["PivQR"] <- alist(RcppEigen::fastLmPure(mm, y, 0L))
22-
## LDLt Cholesky decomposition with rank detection
23-
exprs["LDLt"] <- alist(RcppEigen::fastLmPure(mm, y, 2L))
24-
## SVD using the Lapack subroutine dgesdd and Eigen support
25-
exprs["GESDD"] <- alist(RcppEigen::fastLmPure(mm, y, 6L))
26-
## SVD (the JacobiSVD class from Eigen)
27-
exprs["SVD"] <- alist(RcppEigen::fastLmPure(mm, y, 4L))
28-
## eigenvalues and eigenvectors of X'X
29-
exprs["SymmEig"] <- alist(RcppEigen::fastLmPure(mm, y, 5L))
30-
31-
## Non-rank-revealing decompositions. These work fine except when
32-
## they don't.
33-
34-
## Unpivoted QR decomposition
35-
exprs["QR"] <- alist(RcppEigen::fastLmPure(mm, y, 1L))
36-
## LLt Cholesky decomposition
37-
exprs["LLt"] <- alist(RcppEigen::fastLmPure(mm, y, 3L))
38-
39-
if (suppressMessages(require("RcppArmadillo", character=TRUE, quietly=TRUE))) {
40-
exprs["arma"] <- alist(RcppArmadillo::fastLmPure(mm, y))
41-
}
10+
if(require("microbenchmark", character=TRUE, quietly=TRUE)){
4211

43-
if (suppressMessages(require("RcppGSL", character=TRUE, quietly=TRUE))) {
44-
exprs["GSL"] <- alist(RcppGSL::fastLmPure(mm, y))
45-
}
12+
## define different versions of lm
13+
exprs <- list()
4614

47-
do_bench <- function(n=100000L, p=40L, nrep=20L, suppressSVD=(n > 100000L)) {
48-
mm <- cbind(1, matrix(rnorm(n * (p - 1L)), nc=p-1L))
49-
y <- rnorm(n)
50-
if (suppressSVD) exprs <- exprs[!names(exprs) %in% c("SVD", "GSL")]
51-
cat("lm benchmark for n = ", n, " and p = ", p, ": nrep = ", nrep, "\n", sep='')
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),]
73-
}
15+
## These versions use rank-revealing decompositions and thus can
16+
## handle rank-deficient cases.
17+
18+
# default version used in lm()
19+
exprs["lm.fit"] <- alist(stats::lm.fit(mm, y))
20+
21+
# versions from RcppEigen
22+
## column-pivoted QR decomposition - similar to lm.fit
23+
exprs["PivQR"] <- alist(RcppEigen::fastLmPure(mm, y, 0L))
24+
## LDLt Cholesky decomposition with rank detection
25+
exprs["LDLt"] <- alist(RcppEigen::fastLmPure(mm, y, 2L))
26+
## SVD using the Lapack subroutine dgesdd and Eigen support
27+
exprs["GESDD"] <- alist(RcppEigen::fastLmPure(mm, y, 6L))
28+
## SVD (the JacobiSVD class from Eigen)
29+
exprs["SVD"] <- alist(RcppEigen::fastLmPure(mm, y, 4L))
30+
## eigenvalues and eigenvectors of X'X
31+
exprs["SymmEig"] <- alist(RcppEigen::fastLmPure(mm, y, 5L))
32+
33+
## Non-rank-revealing decompositions. These work fine except when
34+
## they don't.
35+
36+
## Unpivoted QR decomposition
37+
exprs["QR"] <- alist(RcppEigen::fastLmPure(mm, y, 1L))
38+
## LLt Cholesky decomposition
39+
exprs["LLt"] <- alist(RcppEigen::fastLmPure(mm, y, 3L))
40+
41+
if (suppressMessages(require("RcppArmadillo", character=TRUE, quietly=TRUE))) {
42+
exprs["arma"] <- alist(RcppArmadillo::fastLmPure(mm, y))
43+
}
7444

75-
print(do_bench())
45+
if (suppressMessages(require("RcppGSL", character=TRUE, quietly=TRUE))) {
46+
exprs["GSL"] <- alist(RcppGSL::fastLmPure(mm, y))
47+
}
7648

77-
sessionInfo()
49+
do_bench <- function(n=100000L, p=40L, nrep=20L, suppressSVD=(n > 100000L)) {
50+
mm <- cbind(1, matrix(rnorm(n * (p - 1L)), nc=p-1L))
51+
y <- rnorm(n)
52+
if (suppressSVD) exprs <- exprs[!names(exprs) %in% c("SVD", "GSL")]
7853

79-
.Call("RcppEigen_eigen_version", FALSE, PACKAGE="RcppEigen")
54+
cat("lm benchmark for n = ", n, " and p = ", p, ": nrep = ", nrep, "\n", sep='')
55+
cat("RcppEigen: Included Eigen version", paste(RcppEigen:::eigen_version(FALSE), collapse="."), "\n")
56+
cat("RcppEigen: Eigen SSE support", RcppEigen:::Eigen_SSE(), "\n")
8057

81-
.Call("RcppEigen_Eigen_SSE", PACKAGE="RcppEigen")
58+
mb <- microbenchmark(list=exprs, times = nrep)
59+
60+
op <- options(microbenchmark.unit="relative")
61+
on.exit(options(op))
62+
63+
mb_relative <- summary(mb)
64+
levels(mb_relative$expr) <- names(exprs)
65+
66+
options(microbenchmark.unit=NULL)
67+
mb_absolute <- summary(mb)
68+
levels(mb_absolute$expr) <- names(exprs)
69+
70+
mb_combined <- merge(mb_relative[, c("expr", "median")],
71+
mb_absolute[, c("expr", "median")],
72+
by="expr")
73+
74+
colnames(mb_combined) <- c("Method",
75+
"Relative",
76+
paste0("Elapsed (", attr(mb_absolute, "unit"), ")"))
77+
78+
mb_combined[order(mb_combined$Relative),]
79+
}
80+
81+
print(do_bench())
82+
}

0 commit comments

Comments
 (0)