R写作风格-require vs. :: [英] R writing style - require vs. ::
问题描述
好的,我们都熟悉R中的双冒号运算符.每当我要编写一些函数时,我都会使用require(<pkgname>)
,但我一直在考虑使用::
.在自定义函数中使用require
比library
更好,因为require
返回警告和FALSE
,而library
与library
不同,如果您提供不存在的程序包名称,则会返回错误.
OK, we're all familiar with double colon operator in R. Whenever I'm about to write some function, I use require(<pkgname>)
, but I was always thinking about using ::
instead. Using require
in custom functions is better practice than library
, since require
returns warning and FALSE
, unlike library
, which returns error if you provide a name of non-existent package.
另一方面,::
运算符从程序包中获取变量,而require
加载整个程序包(至少我希望如此),所以我首先想到了速度差异. ::
必须比require
快.
On the other hand, ::
operator gets the variable from the package, while require
loads whole package (at least I hope so), so speed differences came first to my mind. ::
must be faster than require
.
然后我做了一些分析以检查-我编写了两个简单的函数,分别从foreign
包和::
分别从foreign
包加载read.systat
函数,因此导入了Iris.syd
数据集附带了foreign
软件包,每个函数重复了1000次(这是无耻的任意选择),并且...计算了一些数字.
And I did some analysis in order to check that - I've written two simple functions that load read.systat
function from foreign
package, with require
and ::
respectively, hence import Iris.syd
dataset that ships with foreign
package, replicated functions 1000 times each (which was shamelessly arbitrary), and... crunched some numbers.
奇怪的是(或没有),我发现用户CPU和运行时间存在显着差异,而系统CPU则没有显着差异.还有一个更奇怪的结论:::
实际上要慢一些! ::
的文档非常陈旧,仅查看源代码,很明显::
应该会表现更好!
Strangely (or not) I found significant differences in terms of user CPU and elapsed time, while there were no significant differences in terms of system CPU. And yet more strange conclusion: ::
is actually slower! Documentation for ::
is very blunt, and just by looking at sources it's obvious that ::
should perform better!
需要
#!/usr/local/bin/r
## with require
fn1 <- function() {
require(foreign)
read.systat("Iris.syd", to.data.frame=TRUE)
}
## times
n <- 1e3
sink("require.txt")
print(t(replicate(n, system.time(fn1()))))
sink()
双冒号
#!/usr/local/bin/r
## with ::
fn2 <- function() {
foreign::read.systat("Iris.syd", to.data.frame=TRUE)
}
## times
n <- 1e3
sink("double_colon.txt")
print(t(replicate(n, system.time(fn2()))))
sink()
获取CSV数据此处.一些统计信息:
Grab CSV data here. Some stats:
user CPU: W = 475366 p-value = 0.04738 MRr = 975.866 MRc = 1025.134
system CPU: W = 503312.5 p-value = 0.7305 MRr = 1003.8125 MRc = 997.1875
elapsed time: W = 403299.5 p-value < 2.2e-16 MRr = 903.7995 MRc = 1097.2005
MRr是require
的平均等级,MRc是::
的平均等级.我一定在这里做错了.只是没有任何意义... ::
的执行时间似乎要快得多!!!我可能已经搞砸了,您不应该放弃该选择...
MRr is mean rank for require
, MRc ibid for ::
. I must have done something wrong here. It just doesn't make any sense... Execution time for ::
seems way faster!!! I may have screwed something up, you shouldn't discard that option...
好的...我浪费了我的时间来看看有什么区别,并且我进行了完全无用的分析,因此,回到问题所在:
OK... I've wasted my time in order to see that there is some difference, and I carried out completely useless analysis, so, back to the question:
"为什么在编写函数时为什么要比::
更喜欢require
?"
"Why should one prefer require
over ::
when writing a function?"
=)
推荐答案
为什么一个人更喜欢要求:: 编写函数时?"
"Why should one prefer require over :: when writing a function?"
由于不错的TRUE/FALSE返回值,因此我通常更喜欢require
,这使我可以处理在进入代码之前无法预先获得软件包的可能性.尽早崩溃,而不是进行分析的一半.
I usually prefer require
due to the nice TRUE/FALSE return value that lets me deal with the possibility of the package not being available up front before getting into the code. Crash as early as possible instead of halfway through your analysis.
仅在需要确保使用的是正确版本的函数时才使用::
,而不是其他掩盖名称的软件包中的版本.
I only use ::
when I need to make sure I am using the correct version of a function, not a version from some other package that is masking the name.
另一方面,::运算符得到 包中的变量,而 需要装载整个包裹(至少 我希望如此),所以速度差异来了 首先在我的脑海. ::必须更快 超出要求.
On the other hand, :: operator gets the variable from the package, while require loads whole package (at least I hope so), so speed differences came first to my mind. :: must be faster than require.
我认为您可能会忽略require
附加foreign
时,并未将其所有内容都加载到内存中.有关延迟加载的完整详细信息,请参见教授. Rips在RNews,第4卷,第2期中的文章.
I think you may be ignoring the effects of lazy loading which is used by the foreign
package according to the first page of its manual. Essentially, packages that use lazy loading defer the loading of objects, such as functions, until the objects are called upon for the first time. So your argument that "::
must be faster than require" is not necessarily true as foreign
is not loading all of its contents into memory when you attach it with require
. For full details on lazy loading, see Prof. Ripley's article in RNews, Volume 4, Issue 2.
这篇关于R写作风格-require vs. ::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!