问题
This should be easy, but can't find any straight-forward answers on google or SO.
Imagine in R I run a function FOuter()
, and within its body, it does some loop and calls another function FInner()
. Is there a simple way of counting/recording the number of times FInner
gets called? I'm trying to estimate how much time I can save if I optimize FInner
.
回答1:
You're looking for trace
.
f1 <- function() 1
f2 <- function() {
for(i in 1:10) f1()
}
.count <- 0
trace(f1, tracer=function() .count <<- .count +1)
f2()
.count
# 10
untrace(f1)
来源:https://stackoverflow.com/questions/21687514/r-count-function-calls