public
Created

benchmarking armamax against apply and pmax

  • Download Gist
benchmark.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.