Skip to content
Prev 205620 / 398506 Next

Naming functions for the purpose of profiling

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: