非常に頻繁に呼び出される関数内にlibrary
/ステートメントを含めることによる悪影響はありますか?require
使用時間はごくわずかに思えますが、数分ごとに関数を呼び出していますが、繰り返しrequire
呼び出しにマイナス面があるかどうか疑問に思っていますか?
この関数は単なる個人的なユーティリティであり、共有されていないことに注意してください。つまり、それを使用しているのは私だけです
ちなみに、なぜlibrary
半分遅いのかについての洞察はありrequire
ますか? 私はそれらが同義であるという印象を受けました。
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