Created
September 9, 2014 17:48
-
-
Save wrathematics/094e361cb94557878d16 to your computer and use it in GitHub Desktop.
Sampling
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
library(Rcpp) | |
body <- " | |
SEXP foo(const int len, Rcpp::NumericVector pr) | |
{ | |
Rcpp::IntegerVector ret(len); | |
RNGScope(); | |
for (int i=0; i<len; i++) | |
{ | |
if (unif_rand() > pr[i]) | |
ret[i] = 1; | |
else | |
ret[i] = 2; | |
} | |
return ret; | |
} | |
" | |
cppFunction(body) | |
library(rbenchmark) | |
DF1 <- data.frame(Pr = runif(100, min = 0, max = 1), Index = 1:100) | |
benchmark(foo(length(DF1$Pr), DF1$Pr), replications = 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment