The Art of R Programming

(WallPaper) #1

argument to stop the monitoring. Finally, we’ll callsummaryRprof()to see the
results.



x <- runif(1000000)
Rprof()
invisible(powers1(x,8))
Rprof(NULL)
summaryRprof()
$by.self
self.time self.pct total.time total.pct
"cbind" 0.74 86.0 0.74 86.0
"*" 0.10 11.6 0.10 11.6
"matrix" 0.02 2.3 0.02 2.3
"powers1" 0.00 0.0 0.86 100.0



$by.total
total.time total.pct self.time self.pct
"powers1" 0.86 100.0 0.00 0.0
"cbind" 0.74 86.0 0.74 86.0
"*" 0.10 11.6 0.10 11.6
"matrix" 0.02 2.3 0.02 2.3


$sampling.time
[1] 0.86


We see immediately that the runtime of our code is dominated by calls
tocbind(), which as we noted in the extended example is indeed slowing
things down.
By the way, the call toinvisible()in this example is used to suppress out-
put. We certainly don’t want to see the 1,000,000-row matrix returned by
powers1()here!
Profilingpowers2()does not show any obvious bottlenecks.



Rprof()
invisible(powers2(x,8))
Rprof(NULL)
summaryRprof()
$by.self
self.time self.pct total.time total.pct
"powers2" 0.38 67.9 0.56 100.0
"matrix" 0.14 25.0 0.14 25.0
"*" 0.04 7.1 0.04 7.1



$by.total
total.time total.pct self.time self.pct
"powers2" 0.56 100.0 0.38 67.9
"matrix" 0.14 25.0 0.14 25.0
"*" 0.04 7.1 0.04 7.1


Performance Enhancement: Speed and Memory 317
Free download pdf