Profiling performance of functions that call other functions

て烟熏妆下的殇ゞ 提交于 2020-01-15 02:47:26

问题


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.

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/13403990/profiling-performance-of-functions-that-call-other-functions

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