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
Naming functions for the purpose of profiling
2 messages · Magnus Torfason
1 day later
I wanted to send an update on my exploits in trying to get rid of
<Anonymous> entries in my Rprof.out file (see my prior mail below).
Basically, I have now found a way to dynamically name a function, and
even if it is not the cleanest way, I think it will help me in some of
my profiling needs, and may help others as well. I'm including some
demonstration code that addresses the issues I wanted to solve:
##########################
# Demonstration of function names in profiling output
a.func <- function() for (i in 1:1000000) b = 100^100
# Here, the function name ends up in Rprof.out
# (This is the straight-forward example)
Rprof("Rprof-simple-a.out")
a.func()
Rprof(NULL)
# This doesn't work
# (Function shows up as '<Anonymous>' in Rprof.out)
b = list()
b$func = a.func
Rprof("Rprof-simple-b.out")
b$func()
Rprof(NULL)
# By assigning to a variable, the anonymous function gets an
# explicit name (Function shows up as 'c.func' in Rprof.out)
c.func = b$func
Rprof("Rprof-simple-c.out")
c.func()
Rprof(NULL)
# This example is a bit contrived, but works nevertheless
# The anonymous function is assigned to an explicit variable,
# but the variable name can by generated dynamically
# (Function shows up as 'd.func' in Rprof.out, and the stack
# also includes calls to 'eval' as expected)
d = list()
d$func = b$func
Rprof("Rprof-simple-d.out")
desired.func.name = "d.func"
eval(parse(text=paste(
desired.func.name," = d$func; ",desired.func.name,"()",
sep="")))
Rprof(NULL)
##########################
And thanks to Jim Holtman who contacted me off-line and gave me some
helpful advice on profiling in general.
Best,
Magnus
On 1/5/2010 2:58 PM, Magnus Torfason wrote:
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