![](/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.