分析调用其他函数的函数的性能 [英] Profiling performance of functions that call other functions
问题描述
我正在编写一个相对复杂的函数来进行从头到尾的数据分析.这个函数调用了我编写的许多子函数(它们本身称为子子函数等).我正在寻找一种工具来告诉我我的功能在每个子功能上花费了多少时间sub-sub-function,为了看看我应该在哪里寻找性能改进.类似于 MATLAB 的 profiler 会很好.
I'm writing a relatively complex function to do a start-to-finish data analysis. This function calls lots of sub-functions I have written (which themselves call sub-sub-functions, etc.). I'm looking for a tool to tell me how much time my function is spending on each sub-function & sub-sub-function, in order to see where I should look for performance improvements. Something analagous to MATLAB's profiler would be nice.
例如,一个假设的函数audit
:
For instance, a hypothetical function audit
:
#fun1 is called by fun2
fun1 <- function() {
rnorm(100000)
}
fun2 <- function(x) {
y <- x+1
z <- y*fun1()
}
mainFun <- function() {
z+3
}
audit(mainFun())
> mainFun = 1 s; of which 95% is in fun1 and 98% is fun2
显然我可以为每个单独的函数使用 microbenchmark()
或 system.time()
- 但有效地使用它变得棘手,因为 mainFun
变得更加复杂.是否有现成的工具?
Obviously I can use microbenchmark()
or system.time()
for each individual function - but using this effectively becomes tricky as the mainFun
becomes more complex. Are there ready-made tools for this?
推荐答案
谷歌搜索 R profiler
将带你到 Rprof
函数,这就是你正在寻找的.本质上,您调用 Rprof()
并正常运行您的脚本.summaryRprof
函数是总结分析器结果的一种方便的方式.有关更多详细信息,请查看 ?Rprof
和关于 整理和分析 R 代码 在 编写 R 扩展,或 这个链接,这个SO问题,甚至这组 SO 问题.
Googling for R profiler
will lead you to the Rprof
function, which is what you are looking for. In essence, you call Rprof()
and run your script as normal. The summaryRprof
function is a convinient way of summarizing the result of the profiler. For more details take a look at ?Rprof
and the chapter on Tidying and profiling R code in Writing R Extensions, or this link, this SO question, or even this set of SO questions.
这篇关于分析调用其他函数的函数的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!