pat shipan
pat shipan

Reputation: 763

lineprof equivalent for Rcpp

The lineprof package in R is very useful for profiling which parts of function take up time and allocate/free memory.

Is there a lineprof() equivalent for Rcpp ?

I currently use std::chrono::steady_clock and such to get chunk timings out of an Rcpp function. Alternatives? Does Rstudio IDE provide some help here?

Upvotes: 5

Views: 685

Answers (2)

Kevin Ushey
Kevin Ushey

Reputation: 21315

To supplement @Dirk's answer...

If you are working on OS X, the Time Profiler Instrument, part of Apple's Instruments set of instrumentation tools, is an excellent sampling profiler.

Just to fix ideas:

  • A sampling profiler lets you answer the question, what code paths does my program spend the most time executing?

  • A (full) cache profiler lets you answer the question, which are the most frequently executed code paths in my program?

These are different questions -- it's possible that your hottest code paths are already optimized enough that, even though the total number of instructions executed in that path is very high, the amount of time required to execute them might be relatively low.

If you want to use instruments to profile C++ code / routines used in an R package, the easiest way to go about this is:

  1. Create a target, pointed at your R executable, with appropriate command line arguments to run whatever functions you wish to profile:

instruments-target

  1. Set the command line arguments to run the code that will exercise your C++ routines -- for example, this code runs Rcpp:::test(), to instrument all of the Rcpp test code:

instruments-arguments

  1. Click the big red Record button, and off you go!

I'll leave the rest of the instructions in understanding instruments + the timing profiler to your google-fu + the documentation, but (if you're on OS X) you should be aware of this tool.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368389

See any decent introduction to high(er) performance computing as eg some slides from (older) presentation of my talks page which include worked examples for both KCacheGrind (part of the KDE frontend to Valgrind) as well as Google Perftools.

In a more abstract sense, you need to come to terms with the fact that C++ != R and not all tools have identical counterparts. In particular Rprof, the R profiler which several CRAN packages for profiling build on top of, is based on the fact that R is interpreted. C++ is not, so things will be different. But profiling compiled is about as old as compiling and debugging so you will find numerous tutorials.

Upvotes: 2

Related Questions