![](/img/trans.png)
[英]How to transform a nested for-loop operation to a more efficient code in R
[英]Efficient code using nested functions in R
我想知道在使用嵌套函数时R中更有效的方法是什么。 本质上,我有三个功能f1,f2,f3。 f3使用f2,而f2本身使用f1
我有2个选项:
据您所知,这些方式中的一种是否比另一种更有效率?
非常感谢
谢谢。 我不知道这个功能。 我仅使用2个函数f1和f2运行了以下命令:
f1 <- function(x) {
y <- x + 2
return(y)
}
f2 <- function(y){
x = 3
z <- y + 3 + f1(x)
return(z)
}
哪个回来了
> microbenchmark::microbenchmark(f2(2))
Unit: nanoseconds
expr min lq mean median uq max neval
f2(2) 737 754 47665.41 798.5 910.5 4667754 100
相对于
f3 <- function(y){
f4 <- function(x) {
y <- x + 2
return(y)}
x = 3
z <- y + 3 + f4(x)
return(z)
}
似乎慢一点
> microbenchmark::microbenchmark(f3(2))
Unit: nanoseconds
expr min lq mean median uq max neval
f3(2) 844 868.5 53053.53 1000 1096 5180886 100
尽管我不确定这个例子是否可靠...因为它很快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.