R version 2.13.1 (2011-07-08) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-unknown-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(rbenchmark) > library(inline) > library(RcppEigen) Loading required package: Rcpp > ## create an R function from Eigen-based C++ code > ff <- if (packageVersion("RcppEigen") > "0.1.1") { # development version + cxxfunction(signature(Xs = "matrix", ys = "numeric"), ' + typedef Eigen::Map MMatrixXd; + typedef Eigen::Map MVectorXd; + + const MMatrixXd Xe(as(Xs)); + const MVectorXd ye(as(ys)); + return wrap(Xe * ye); + ', plugin = "RcppEigen") + } else { + cxxfunction(signature(Xs = "matrix", ys = "numeric"), ' + typedef Eigen::Map MMatrixXd; + typedef Eigen::Map MVectorXd; + + const NumericMatrix X(Xs); + const NumericVector y(ys); + const MMatrixXd Xe(X.begin(), X.rows(), X.cols()); + const MVectorXd ye(y.begin(), y.size()); + Eigen::VectorXd res = Xe * ye; + return NumericVector(res.data(), res.data() + res.size()); + ', plugin = "RcppEigen") + } > > set.seed(1) # for reproducible results > m <- 100 > n <- 10 > A <- matrix(runif(m * n), ncol = n) > y <- runif(n) > all.equal(as.vector(A %*% y), ff(A, y)) [1] TRUE > benchmark(Rcode = expression(A %*% y), Eigen = ff(A, y), replications=100000) test replications elapsed relative user.self sys.self user.child sys.child 2 Eigen 100000 0.808 1.000000 0.808 0.001 0 0 1 Rcode 100000 1.037 1.283416 1.031 0.006 0 0 > m <- 1000 > n <- 1000 > A <- matrix(runif(m * n), ncol = n) > y <- runif(n) > all.equal(as.vector(A %*% y), ff(A, y)) [1] TRUE > benchmark(Rcode = expression(A %*% y), Eigen = ff(A, y), replications=1000) test replications elapsed relative user.self sys.self user.child sys.child 2 Eigen 1000 1.957 1.000000 1.956 0 0 0 1 Rcode 1000 6.837 3.493613 6.835 0 0 0 > > proc.time() user system elapsed 14.178 0.388 14.527