Skip to content

Instantly share code, notes, and snippets.

@floswald
Created May 17, 2013 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floswald/5600138 to your computer and use it in GitHub Desktop.
Save floswald/5600138 to your computer and use it in GitHub Desktop.
benchmarking armamax against apply and pmax
library(inline)
library(Rcpp)
cpp <- 'arma::mat A = Rcpp::as<arma::mat>(X_);
arma::vec y(A.n_rows);
arma::rowvec tmp(A.n_cols);
for (int i=0; i<A.n_rows; i++) {
tmp = A.row(i);
y(i) = tmp.max();
}
return wrap(y);
'
cat('rows >> columns')
armaf <- cxxfunction(signature(X_="matrix"),body=cpp,plugin="RcppArmadillo")
A = matrix(rnorm(400000),2000,50)
Ad = as.data.frame(A)
all.equal(do.call(pmax,as.data.frame(A)),as.numeric(armaf(A)))
benchmark(R.do.call=do.call(pmax,Ad),armamax=armaf(A),replications=100)
benchmark(R.apply=apply(A,1,max),armamax=armaf(A),replications=100)
cat('columns >> rows')
A = matrix(rnorm(400000),50,2000)
Ad = as.data.frame(A)
all.equal(do.call(pmax,as.data.frame(A)),as.numeric(armaf(A)))
benchmark(R.do.call=do.call(pmax,Ad),armamax=armaf(A),replications=100)
benchmark(R.apply=apply(A,1,max),armamax=armaf(A),replications=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment