17
Are there any adverse effect to including library/require statements inside of functions that will be called very frequently?
The time used seems rather negligble, but I am calling the function every few minutes and I am wondering if there is any downside to the repetitve require calls?
note that the function is just a personal util and is not being shared. ie, I am the only one using it
Incidentally, any insight as to why library is half as slow as require? I was under the impression they were synonymous.
WithREQUIRE <- function(x) {
require(stringr)
str_detect(x, "hello")
}
WithLIBRARY <- function(x) {
library(stringr)
str_detect(x, "hello")
}
Without <- function(x) {
str_detect(x, "hello")
}
x <- "goodbye"
library(rbenchmark)
benchmark(WithREQUIRE(x), WithLIBRARY(X), Without(x), replications=1e3, order="relative")
# test replications elapsed relative user.self sys.self
# Without(x) 1000 0.592 1.000 0.262 0.006
# WithREQUIRE(x) 1000 0.650 1.098 0.295 0.015
# WithLIBRARY(X) 1000 1.359 2.296 0.572 0.024