Rcpp Version 1.0.9
piBySimulation.r
Go to the documentation of this file.
1 #!/usr/bin/env r
2 
3 library(Rcpp)
4 library(rbenchmark)
5 
6 piR <- function(N) {
7  x <- runif(N)
8  y <- runif(N)
9  d <- sqrt(x^2 + y^2)
10  return(4 * sum(d < 1.0) / N)
11 }
12 
13 sourceCpp("piSugar.cpp")
14 
15 N <- 1e6
16 
17 set.seed(42)
18 resR <- piR(N)
19 
20 set.seed(42)
21 resCpp <- piSugar(N)
22 
23 ## important: check results are identical with RNG seeded
24 stopifnot(identical(resR, resCpp))
25 
26 res <- benchmark(piR(N), piSugar(N), order="relative")
27 
28 print(res[,1:4])