There is a performance test between R functions, R functions with byte compiler, Rcpp and RcppArmadillo. It is about the generating the normal random variables and the vectorized computations.
Code:
1 | # 1 |
Number 1, 2, 3, 4, 5 and 6 are the R functions. Number 14, 15, 16, 17 and 18 are R functions with byte compiler. Number 7, 8, 9, 10 and 11 are the Rcpp functions. Number 12 and 13 are RcppArmadillo functions.
Number 1 function is using for-loop in R. Number 2 function is using the function pmax (R vectorized maximum function). Number 3, 4 and 5 functions are the application of sapply, apply and lapply function, respectively. Number 6 function is using the parallel computing package snowfall in R.
Number 7 and 8 function is to locate the two vectors of normal r.v. and to use Rcpp function to calculate. The difference between two functions are the input element, number 7 function input two vectors and number 8 function input one matrix.
Number 9 and 10 functions is the parallel version of number 7 and 8 functions, respectively. Number 11 function is to use the c++ for locate the two vectors of normal r.v. and to calculate in the way of number 9 function. Number 12 and 13 functions are to use armadillo for locating the two vectors of normal r.v. and calculating with vector or matrix manipulation.
The results shows that generating normal r.v. in c++ is fast than generating in R, the vectorized function pmax is fast than other ?apply functions and calculation of two vectors are fast than a matrix (I think that the reason is searching address.).
My environment is ubuntu 14.04, R 3.1.1 compiled by intel c++, fortran compiler with MKL. My CPU is 3770K@4.3GHz.
Remark:
I think that the reason why the calculation with matrix is slow is that c++ is a row-major language, so I try another example:
1 | library(Rcpp) |
The results show that calculation in row and calculation in column is big difference. My environment is ubuntu 14.04, R 3.1.1 compiled by intel c++, fortran compiler with MKL. My CPU is 3770K@4.3GHz.