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?

1 Answer 1

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.

2
  • Some good discussion of R profiling here, too: stackoverflow.com/questions/3650862/…
    – Drew Steen
    Nov 15, 2012 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. Nov 15, 2012 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.