[Rcpp-devel] Cost of RCPP_EXPOSED_CLASS pre/post population (post substantially lower)
Thell Fowler
tbfowler4 at gmail.com
Thu Feb 27 02:27:18 CET 2014
Possibly interesting results; at least to anyone who may typically
construct and populate a class in the constructor...
> Rcpp::sourceCpp('VectorCreation.cpp')
> v1 <- function(len) {v<-make_preVector(len); v$vec }
> v2 <- function(len) {v<-make_postVector(); v$populate(len); v$vec }
> identical( v1(10), v2(10) )
[1] TRUE
> library(microbenchmark)
> microbenchmark( v1(6250000), v2(6250000), times=20 )
Unit: milliseconds
expr min lq median uq max neval
v1(6250000) 11.556459 11.912062 17.983704 24.14962 45.93693 20
v2(6250000) 9.158329 9.338543 9.516148 15.83003 40.87731 20
_File: VectorCreation.cpp <gist:: https://gist.github.com/Thell/9242382 >
// [[Rcpp::plugins("cpp11")]]
#include <RcppCommon.h>
class preVector;
RCPP_EXPOSED_CLASS(preVector)
class postVector;
RCPP_EXPOSED_CLASS(postVector)
#include <Rcpp.h>
using namespace Rcpp;
class preVector {
public:
std::vector< int > vec;
preVector(int len) : vec(len)
{
int i(0);
while( i < len ) {
vec[i] = i;
i++;
}
}
};
preVector make_preVector( int len ) { return preVector(len); }
RCPP_MODULE(preVector){
class_< preVector >("preVector")
.constructor< int >()
.field("vec", &preVector::vec)
;
function( "make_preVector", &make_preVector );
}
class postVector {
public:
std::vector< int > vec;
postVector() {}
void populate(int len) {
vec = std::vector< int >(len);
int i(0);
while( i < len ) {
vec[i] = i;
i++;
}
}
};
postVector make_postVector() { return postVector(); }
RCPP_MODULE(postVector){
class_< postVector >("postVector")
.constructor()
.method("populate", &postVector::populate )
.field("vec", &postVector::vec)
;
function( "make_postVector", &make_postVector );
}
/*** R
v1 <- function(len) {v<-make_preVector(len); v$vec }
v2 <- function(len) {v<-make_postVector(); v$populate(len); v$vec }
identical( v1(10), v2(10) )
library(microbenchmark)
microbenchmark( v1(6250000), v2(6250000), times=20 )
*/
--
Sincerely,
Thell
More information about the Rcpp-devel
mailing list