sapply 与复合函数的速度比较
> system.time(sapply(rnorm(1000000,0,1), function (x) round(x,2)))
user system elapsed
2.78 0.11 2.89
> system.time(round(rnorm(1000000,0,1),2))
user system elapsed
0.29 0.00 0.30
在阅读了 R Tips 问题的答案后,我尝试了这个。我没想到 sapply 会比上述情况下的等效复合函数慢几个数量级。有谁知道为什么会这样?如果我理解正确,sapply 将会矢量化并且接近最佳速度。
> system.time(sapply(rnorm(1000000,0,1), function (x) round(x,2)))
user system elapsed
2.78 0.11 2.89
> system.time(round(rnorm(1000000,0,1),2))
user system elapsed
0.29 0.00 0.30
I was trying this out after reading the answers to the R tips question. I did not expect sapply to be order of magnitude slower than the equivalent composite function in the above case. Does anyone know why this is the case? If i understand correctly sapply will vectorize and be near optimally fast.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sapply 可能是 lapply 的简单包装,没有矢量化。尝试此代码:
并在此处查看实现: https://svn .r-project.org/R/trunk/src/main/apply.c
probably sapply, which is a simple wrapper of lapply, is not vectorized. try this code:
and see the implementation here: https://svn.r-project.org/R/trunk/src/main/apply.c
这里没有什么可以应用的 - 你只给它一个向量 - 而不是向量列表,并且 sapply 将结果转换为(单列)矩阵。
sapply 正在为您简化结果,但这样做必须生成一个数组。
如果给它一个列表进行比较:
There's nothing here to sapply to - you only give it a single vector - not a list of vectors, and sapply converts the result to a (single column) matrix.
sapply is simplifying the result for you, but in doing so has to generate an array.
Compare if you give it a list: