[R] Deleting for() loop in function

jim holtman jholtman at gmail.com
Wed Oct 10 20:17:47 CEST 2007


There are a couple of outputs from Rprof.  One shows time spent in the
function (and everything that it calls) and the other in the function
itself.

What I do is to post process the output from Rprof into a format that
is easier to understand.  In this case, the Rprof run took 12.2
seconds of which 7.2 were spent in an 'lapply'.  Within the 'lapply',
6.9 seconds was spent in 'read.table' to read in the data.  This shows
what the calling tree of the functions are and the time spent in them
and their children.  If you have perl on your system, I can send you
the script.  The same information is available from summaryRprof, but
may be a little harder to interprete.  This would indicate that the
problem is the 'lapply', but it is really what is going on inside it.

perl c:/perf/bin/readrprof.pl rprof.out
  0  12.2 root
  1.    7.2 lapply
  2. .    7.2 FUN
  3. . .    6.9 read.table
  4. . . .    3.4 scan
  4. . . .    2.1 type.convert
  5. . . . .    0.1 character
  6. . . . . .    0.1 vector
  4. . . .    0.3 sapply
  5. . . . .    0.2 unique
  6. . . . . .    0.1 unlist
  7. . . . . . .    0.0 lapply
  8. . . . . . . .    0.0 match.fun
  6. . . . . .    0.0 unique.default
  7. . . . . . .    0.0 inherits
  5. . . . .    0.0 lapply
  6. . . . . .    0.0 FUN
  5. . . . .    0.0 unlist


On 10/10/07, Alberto Monteiro <albmont at centroin.com.br> wrote:
>
> Jim Holtman wrote:
> >
> > One of the things that you should do is to use Rprof
> > to see where time is being spent.
> >
> Rprof is great! I didn't know such functionality existed.
>
> But I am a very destructive user; I think I found a way
> to break Rprof: if I do
>
> f <- my.slow.function
>
> then call
>
> f(...)
>
> Rprof will report that f (and not my.slow.function) was
> called <evil grin>.
>
> I noticed that because I "tested" Rprof with one of my
> programs that do test cases; these test cases run with
> an array of functions, by calling each function from
> the array - precisely what can't be traced by Rprof :-)
>
> Alberto Monteiro
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list