Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nkurz
Created September 9, 2015 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkurz/8fa6ff3772a054294531 to your computer and use it in GitHub Desktop.
Save nkurz/8fa6ff3772a054294531 to your computer and use it in GitHub Desktop.
### compare times for sample.int() vs internal function sample2()
compareSampleTimes = function(popSizeList=c(1e5, 1e6, 1e7, 1e8, 1e9),
sampleSizeList=c(10, 100, 1000, 10000),
numReplications=1000) {
for (sampleSize in sampleSizeList) {
for (popSize in popSizeList) {
elapsed1 = system.time(replicate(numReplications, sample.int(popSize, sampleSize)))[["elapsed"]]
elapsed2 = system.time(replicate(numReplications, .Internal(sample2(popSize, sampleSize))))[["elapsed"]]
cat(sprintf("Sample %d from %.0e: %f vs %f seconds\n", sampleSize, popSize, elapsed1, elapsed2))
}
cat("\n")
}
}
compareSampleTimes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment