-
-
Save joshuaulrich/5d4af930df51ce71e9472d298a572729 to your computer and use it in GitHub Desktop.
getOption benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getOptionOld <- function(x, default = NULL) { | |
if(missing(default) || x %in% names(options())) | |
.Internal(getOption(x)) | |
else | |
default | |
} | |
getOptionNew <- function(x, default = NULL) { | |
ans <- .Internal(getOption(x)) | |
if(is.null(ans)) default else ans | |
} | |
bench <- microbenchmark::microbenchmark( | |
old_found = getOptionOld("width"), | |
old_missing = getOptionOld("blah"), | |
old_missing_default = getOptionOld("blah", "foo"), | |
new_found = getOptionNew("width"), | |
new_missing = getOptionNew("blah"), | |
new_missing_default = getOptionNew("blah", "foo") | |
) | |
bench | |
# Unit: nanoseconds | |
# expr min lq mean median uq max neval | |
# old_found 0 293 431.26 294.0 587.0 880 100 | |
# old_missing 293 294 680.53 587.0 587.0 11437 100 | |
# old_missing_default 92373 93399 123949.44 98237.5 100437.5 2642732 100 | |
# new_found 0 293 451.90 294.0 587.0 2933 100 | |
# new_missing 293 294 16991.01 587.0 733.0 1639832 100 | |
# new_missing_default 293 294 536.86 586.0 587.0 1174 100 | |
# Would need to lookup a missing option with a default 10,000 times before this | |
# would take more than ~1 second. | |
system.time({ i <- 1; while((i <- i + 1) <= 10000) getOptionOld("blah", "foo") }) | |
# user system elapsed | |
# 1.01 0.00 1.02 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment