Rcpp Version 0.12.12
benchmark.r
Go to the documentation of this file.
1 #!/usr/bin/env r
2 #
3 # Comparison benchmark
4 #
5 # This shows how Armadillo improves on the previous version using GNU GSL,
6 # and how both are doing better than lm.fit()
7 #
8 # Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
9 #
10 # This file is part of Rcpp.
11 #
12 # Rcpp is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # Rcpp is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
24 
25 suppressMessages(library(RcppGSL))
26 suppressMessages(library(RcppArmadillo))
27 
28 source("lmArmadillo.R")
29 source("lmGSL.R")
30 
31 set.seed(42)
32 n <- 5000
33 k <- 9
34 X <- cbind( rep(1,n), matrix(rnorm(n*k), ncol=k) )
35 truecoef <- 1:(k+1)
36 y <- as.numeric(X %*% truecoef + rnorm(n))
37 
38 N <- 100
39 
40 lmgsl <- lmGSL()
41 lmarma <- lmArmadillo()
42 
43 tlm <- mean(replicate(N, system.time( lmfit <- lm(y ~ X - 1) )["elapsed"]), trim=0.05)
44 tlmfit <- mean(replicate(N, system.time(lmfitfit <- lm.fit(X, y))["elapsed"]), trim=0.05)
45 tlmgsl <- mean(replicate(N, system.time(lmgsl(y, X))["elapsed"]), trim=0.05)
46 tlmarma <- mean(replicate(N, system.time(lmarma(y, X))["elapsed"]), trim=0.05)
47 
48 res <- c(tlm, tlmfit, tlmgsl, tlmarma)
49 data <- data.frame(results=res, ratios=tlm/res)
50 rownames(data) <- c("lm", "lm.fit", "lmGSL", "lmArma")
51 cat("For n=", n, " and k=", k, "\n", sep="")
52 print(t(data))
53 print(t(1/data[,1,drop=FALSE])) # regressions per second
54