The Art of R Programming

(WallPaper) #1
$sampling.time
[1] 0.56

What aboutpowers3(), the promising approach that didn’t pan out?

> Rprof()
> invisible(powers3(x,8))
> Rprof(NULL)
> summaryRprof()
$by.self
self.time self.pct total.time total.pct
"FUN" 0.94 56.6 0.94 56.6
"outer" 0.72 43.4 1.66 100.0
"powers3" 0.00 0.0 1.66 100.0

$by.total
total.time total.pct self.time self.pct
"outer" 1.66 100.0 0.72 43.4
"powers3" 1.66 100.0 0.00 0.0
"FUN" 0.94 56.6 0.94 56.6

$sampling.time
[1] 1.66

The function logging the largest amount of time wasFUN(), which as
noted in our extended example is simply multiplication. For each pair of
elements ofxhere, one of the elements is multiplied by the other; that is, a
product of two scalars is found. In other words, no vectorization! No wonder
it was slow.

14.4.2 How Rprof() Works.............................................


Let’s explore in a bit more detail whatRprof()does. Every 0.02 seconds (the
default value), R inspects the call stack to determine which function calls
are in effect at that time. It writes the result of each inspection to a file, by
defaultRprof.out. Here is an excerpt of that file from our run ofpowers3():

...
"outer" "powers3"
"outer" "powers3"
"outer" "powers3"
"FUN" "outer" "powers3"
"FUN" "outer" "powers3"
"FUN" "outer" "powers3"
"FUN" "outer" "powers3"
...

318 Chapter 14

Free download pdf