7 // assign to C++ vector
8 std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
10 for (size_t i=0; i<n; i++) {
15 funSerial <- cxxfunction(signature(xs="numeric"), body=serialCode, plugin="Rcpp")
18 std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
19 std::transform(x.begin(), x.end(), x.begin(), ::log);
22 funSerialStdAlg <- cxxfunction(signature(xs="numeric"), body=serialStdAlgCode, plugin="Rcpp")
24 ## same, but with Rcpp vector just to see if there is measurable difference
26 // assign to C++ vector
27 Rcpp::NumericVector x = Rcpp::NumericVector(xs);
29 for (size_t i=0; i<n; i++) {
34 funSerialRcpp <- cxxfunction(signature(xs="numeric"), body=serialRcppCode, plugin="Rcpp")
36 serialStdAlgRcppCode <- '
37 Rcpp::NumericVector x = Rcpp::NumericVector(xs);
38 std::transform(x.begin(), x.end(), x.begin(), ::log);
41 funSerialStdAlgRcpp <- cxxfunction(signature(xs="numeric"), body=serialStdAlgRcppCode, plugin="Rcpp")
43 serialImportTransRcppCode <- '
44 Rcpp::NumericVector x(xs);
45 return Rcpp::NumericVector::import_transform(x.begin(), x.end(), ::log);
47 funSerialImportTransRcpp <- cxxfunction(signature(xs="numeric"), body=serialImportTransRcppCode, plugin="Rcpp")
49 ## now with a sugar expression with internalizes the loop
51 // assign to C++ vector
52 Rcpp::NumericVector x = log ( Rcpp::NumericVector(xs) );
55 funSugarRcpp <- cxxfunction(signature(xs="numeric"), body=sugarRcppCode, plugin="Rcpp")
57 ## lastly via OpenMP for parallel use
59 // assign to C++ vector
60 std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
62 #pragma omp parallel for shared(x, n)
63 for (size_t i=0; i<n; i++) {
69 ## modify the plugin for Rcpp to support OpenMP
70 settings <- getPlugin("Rcpp")
71 settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)
72 settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)
74 funOpenMP <- cxxfunction(signature(xs="numeric"), body=openMPCode, plugin="Rcpp", settings=settings)
78 res <- benchmark(funSerial(z), funSerialStdAlg(z),
79 funSerialRcpp(z), funSerialStdAlgRcpp(z),
80 funSerialImportTransRcpp(z),
81 funOpenMP(z), funSugarRcpp(z),
82 columns=c("test", "replications", "elapsed",
83 "relative", "user.self", "sys.self"),