Skip to content

Instantly share code, notes, and snippets.

@wrathematics
Created September 9, 2014 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrathematics/094e361cb94557878d16 to your computer and use it in GitHub Desktop.
Save wrathematics/094e361cb94557878d16 to your computer and use it in GitHub Desktop.
Sampling
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