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