Created
February 27, 2014 01:20
-
-
Save Thell/9242382 to your computer and use it in GitHub Desktop.
Example of creation time for pre/post construction populated RCPP_EXPOSED_CLASS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [[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(2000000), v2(2000000), times=20 ) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment