[R] Naming functions for the purpose of profiling

Magnus Torfason zulutime.net at gmail.com
Tue Jan 5 20:58:54 CET 2010


Hi all,

I have some long-running code that I'm trying to profile. I am seeing a 
lot of time spent inside the <Anonymous> function. Of course, this can 
in fact be any of several functions, but I am unable to see how I could 
use the information from Rprof.out to discern which function is taking 
the most time. An example line from my Rprof.out is:

rbernoulli <Anonymous> runOneRound FUN lapply sfLapply doTryCatch 
tryCatchOne tryCatchList tryCatch try eval.with.vis eval.with.vis source

In my case, the <Anonymous> functions seem to be any of several 
work-horse functions, that are part of a list, and different cases are 
dispatched to different functions. I could of course get them all out of 
the list and rewrite the dispatch code, but that does not seem like a 
neat way do address this. So I am wondering if there is any way to 
explicitly set the name of a function in a way that leads to it being 
picked up by the profiler?

The same problem seems to apply to <Anonymous> functions that are 
generated on the fly in an apply call or elsewhere.

Of course, having source files and line numbers included in the 
profiling output would solve this issue, but it seems to me that R 
probably does not have any awareness of where a function was "written 
down" (and of course, even the text of a function can be constructed 
dynamically within a program). So I guess that is not a viable approach.

Thanks in advance for any help on this, and any pointers on the best 
references for advanced profiling issues would be appreciated as well (I 
know of summaryRprof of course, but it can be difficult to get the full 
picture from the summaryRprof output if the calling structure is 
complicated).

Best,
Magnus



More information about the R-help mailing list