R计数函数调用 [英] R count function calls

查看:78
本文介绍了R计数函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易,但是在google或SO上找不到简单的答案。

This should be easy, but can't find any straight-forward answers on google or SO.

想象一下在RI中运行一个函数 FOuter(),并在其体内进行一些循环并调用另一个函数 FInner()。是否有一种简单的方法来计数/记录 FInner 被调用的次数?我正在尝试估算如果优化 FInner 可以节省多少时间。

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.

推荐答案

您正在寻找 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)

这篇关于R计数函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆