R count function calls

感情迁移 提交于 2019-12-10 20:07:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!