55# # This file is part of RcppEigen.
66
77require(" stats" , character = TRUE , quietly = TRUE )
8- require(" rbenchmark " , character = TRUE , quietly = TRUE )
8+ require(" microbenchmark " , character = TRUE , quietly = TRUE )
99require(" 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
3939if (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
4343if (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
4747do_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
5975print(do_bench())
0 commit comments