2

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?

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Drew Steen
  • 16,045
  • 12
  • 62
  • 90

1 Answers1

5

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.

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Some good discussion of R profiling here, too: http://stackoverflow.com/questions/3650862/how-to-efficiently-use-rprof-in-r – Drew Steen Nov 15 '12 at 18:50
  • + I've used `rprof`, and I was pleased (which is going some). I don't care for the summary tool. I just grab the result file with the stack traces in it and examine those. – Mike Dunlavey Nov 15 '12 at 20:23