Rcpp Version 1.0.14
Loading...
Searching...
No Matches
piBySimulation.r
Go to the documentation of this file.
1#!/usr/bin/env r
2
3library(Rcpp)
4library(rbenchmark)
5
6piR <- 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
13sourceCpp("piSugar.cpp")
14
15N <- 1e6
16
17set.seed(42)
18resR <- piR(N)
19
20set.seed(42)
21resCpp <- piSugar(N)
22
23## important: check results are identical with RNG seeded
24stopifnot(identical(resR, resCpp))
25
26res <- benchmark(piR(N), piSugar(N), order="relative")
27
28print(res[,1:4])