Skip to content

Instantly share code, notes, and snippets.

@Sleepingwell
Last active December 19, 2015 23:39
Show Gist options
  • Save Sleepingwell/6036328 to your computer and use it in GitHub Desktop.
Save Sleepingwell/6036328 to your computer and use it in GitHub Desktop.
Comparison of performance (speed) or R's Gausian random number generators.
test.func <- function(n.samples) {
nms <- c("Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister", "Knuth-TAOCP-2002", "Knuth-TAOCP", "L'Ecuyer-CMRG")
tmp <- lapply(nms, function(unif.rng.name) {
RNGkind(unif.rng.name)
nms <- c("Kinderman-Ramage", "Ahrens-Dieter", "Box-Muller", "Inversion")
tmp <- sapply(nms, function(norm.rng.name) {
RNGkind(, norm.rng.name)
system.time(rnorm(n.samples))
})[1:3,]
# included here because I'm not sure if rziggurat uses the unif rng internally.
cbind(rziggurat=system.time(rziggurat(n.samples))[1:3], tmp)
})
names(tmp) <- nms
tmp
}
library(SuppDists)
n.samples <- 1e8
times <- test.func(n.samples)
## Results
#
# $`Wichmann-Hill`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.72 16.68 13.00 21.22 21.80
# sys.self 0.85 0.11 0.37 0.29 1.05
# elapsed 2.57 16.82 13.48 21.51 22.91
#
# $`Marsaglia-Multicarry`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.59 7.24 7.03 16.02 12.90
# sys.self 0.49 0.16 0.08 0.16 0.18
# elapsed 2.09 7.44 7.11 16.19 13.11
#
# $`Super-Duper`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.69 7.30 7.23 16.47 12.95
# sys.self 0.48 0.12 0.11 0.12 0.17
# elapsed 2.17 7.44 7.35 16.64 13.12
#
# $`Mersenne-Twister`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.73 7.80 7.66 16.69 13.52
# sys.self 0.45 0.17 0.09 0.19 0.20
# elapsed 2.18 7.99 7.75 16.92 13.73
#
# $`Knuth-TAOCP-2002`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.70 9.28 8.10 16.95 14.26
# sys.self 0.32 0.11 0.09 0.16 0.21
# elapsed 2.02 9.39 8.21 17.13 14.47
#
# $`Knuth-TAOCP`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.69 9.03 8.10 17.01 14.23
# sys.self 0.45 0.04 0.08 0.36 0.30
# elapsed 2.14 9.09 8.19 17.38 14.55
#
# $`L'Ecuyer-CMRG`
# rziggurat Kinderman-Ramage Ahrens-Dieter Box-Muller Inversion
# user.self 1.69 18.34 14.40 22.98 22.56
# sys.self 0.92 0.29 0.26 0.42 0.38
# elapsed 2.61 18.66 14.73 23.46 22.93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment