- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
一些 R 函数会让 R 在函数调用后复制对象,如 nrow,而另一些则不会,如 sum。
例如下面的代码:
x = as.double(1:1e8)
system.time(x[1] <- 100)
y = sum(x)
system.time(x[1] <- 200) ## Fast (takes 0s), after calling sum
foo = function(x) {
return(sum(x))
}
y = foo(x)
system.time(x[1] <- 300) ## Slow (takes 0.35s), after calling foo
最佳答案
我绝对推荐哈德利的 Advanced R本书,因为它深入研究了您可能会发现有趣和相关的一些内部结构。与您的问题最相关(正如@joran 和@lmo 所提到的),速度变慢的原因是强制复制修改的附加引用。
可能对 Memory#Modification 有益的摘录:
There are two possibilities:
R modifies x in place.
R makes a copy of x to a new location, modifies the copy, and then uses the name x to point to the new location.
It turns out that R can do either depending on the circumstances. In the example above, it will modify in place. But if another variable also points to x, then R will copy it to a new location. To explore what’s going on in greater detail, we use two tools from the pryr package. Given the name of a variable, address() will tell us the variable’s location in memory and refs() will tell us how many names point to that location.
pryr
包还具有以更简单的方式处理这些内部结构的工具。
While determining that copies are being made is not hard, preventing such behaviour is. If you find yourself resorting to exotic tricks to avoid copies, it may be time to rewrite your function in C++, as described in Rcpp.
关于R 无故复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42559356/
我有一个小型 Java 11 示例,其中包含 JUnit 5 测试,结果如下: changed conditional boundary → SURVIVED 主类: public final cla
我是一名优秀的程序员,十分优秀!