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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
suppressMessages(library(Rcpp))
suppressMessages(library(inline))
suppressMessages(library(rbenchmark))
## NOTE: Within this section, the new way to compile Rcpp code inline has been
## written. Please use the code next as a template for your own project, and
## do NOT use the old code below
cppFunction('
NumericVector rcppGamma(NumericVector x){
int n = x.size();
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = R::rgamma(3.0, 1.0/(y*y+4));
}
// Return to R
return x;
}')
## This approach is a bit sloppy. Generally, you will want to use
## sourceCpp() if there are additional includes that are required.
cppFunction('
NumericVector gslGamma(NumericVector x){
int n = x.size();
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = gsl_ran_gamma(r,3.0,1.0/(y*y+4));
}
gsl_rng_free(r);
// Return to R
return x;
}', includes = '#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>',
depends = "RcppGSL")
cppFunction('
NumericVector rcppNormal(NumericVector x){
int n = x.size();
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = R::rnorm(1.0/(y+1),1.0/sqrt(2*y+2));
}
// Return to R
return x;
}')
## Here we demonstrate the use of sourceCpp() to show the continuity
## of the code artifact.
sourceCpp(code = '
#include <RcppGSL.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
using namespace Rcpp;
// [[Rcpp::depends("RcppGSL")]]
// [[Rcpp::export]]
NumericVector gslNormal(NumericVector x){
int n = x.size();
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = 1.0/(y+1)+gsl_ran_gaussian(r,1.0/sqrt(2*y+2));
}
gsl_rng_free(r);
// Return to R
return x;
}')
x <- rep(NA, 1e6)
res <- benchmark(rcppGamma(x),
gslGamma(x),
rcppNormal(x),
gslNormal(x),
columns=c("test", "replications", "elapsed", "relative", "user.self", "sys.self"),
order="relative",
replications=20)
print(res)
##
##
## Old code below. Do not use in new projects, it is here solely for comparison
##
##
## NOTE: This is the old way to compile Rcpp code inline.
## The code here has left as a historical artifact and tribute to the old way.
## Please use the code under the "new" inline compilation section.
rcppGamma_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
NumericVector x(xs);
int n = x.size();
RNGScope scope; // Initialize Random number generator. Not needed when Attributes used.
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = ::Rf_rgamma(3.0, 1.0/(y*y+4));
}
// Return to R
return x;
')
gslGamma_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
include='#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>',
body='
NumericVector x(xs);
int n = x.size();
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = gsl_ran_gamma(r,3.0,1.0/(y*y+4));
}
gsl_rng_free(r);
// Return to R
return x;
')
rcppNormal_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
NumericVector x(xs);
int n = x.size();
RNGScope scope; // Initialize Random number generator. Not needed when Attributes used.
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = ::Rf_rnorm(1.0/(y+1),1.0/sqrt(2*y+2));
}
// Return to R
return x;
')
gslNormal_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
include='#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>',
body='
NumericVector x(xs);
int n = x.size();
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
const double y = 1.234;
for (int i=0; i<n; i++) {
x[i] = 1.0/(y+1)+gsl_ran_gaussian(r,1.0/sqrt(2*y+2));
}
gsl_rng_free(r);
// Return to R
return x;
')
|