Skip to content

Instantly share code, notes, and snippets.

@joshuaulrich joshuaulrich/bench.R forked from jimhester/bench.R
Last active Mar 29, 2018

Embed
What would you like to do?
getOption benchmark
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
You can’t perform that action at this time.