library(RcppOctave)
require(rbenchmark) # to benchmark examples
## define a Gamma RNG draw function
o_source(text="
function x = orndg(a, b, n)
x = b * randg(a, n, 1);
end
")
## define a Normal RNG draw function
o_source(text="
function x = orndn(n)
x = randn(n,1);
end
")
N <- 500
set.seed(42) # reset RNG
x1 <- c(.O$orndg(9,1,N))
set.seed(42) # reset RNG
y1 <- rgamma(N,9,1)
stopifnot(all.equal(x1, y1))
res <- benchmark(.O$orndg(9,1,N),
rgamma(N,9,1),
o_rgamma(9,N,1),
columns = c("test", "replications", "elapsed", "relative"),
order="relative",
replications=1000)
print(res)
test replications elapsed relative
2 rgamma(N, 9, 1) 1000 0.072 1.000
3 o_rgamma(9, N, 1) 1000 0.807 11.208
1 .O$orndg(9, 1, N) 1000 1.900 26.389
set.seed(42) # reset RNG
x1 <- c(.O$orndn(N,1))
set.seed(42) # reset RNG
y1 <- rnorm(N)
stopifnot(all.equal(x1, y1))
res <- benchmark(.O$orndn(N),
rnorm(N),
o_rnorm(N,1),
columns = c("test", "replications", "elapsed", "relative"),
order="relative",
replications=1000)
print(res)
test replications elapsed relative
2 rnorm(N) 1000 0.057 1.000
3 o_rnorm(N, 1) 1000 0.092 1.614
1 .O$orndn(N) 1000 1.913 33.561