The Art of R Programming

(WallPaper) #1
But things can get very subtle when it comes to R performance issues, so
let’s try it out.

> m <- 5000
> n <- 1000
> z <- list()
> for (i in 1:m) z[[i]] <- sample(1:10,n,replace=T)
> system.time(for (i in 1:m) z[[i]][3] <- 8)
user system elapsed
0.288 0.024 0.321
> z <- matrix(sample(1:10,m*n,replace=T),nrow=m)
> system.time(z[,3] <- 8)
user system elapsed
0.008 0.044 0.052

Except for system time (again), the matrix formulation did better.
One of the reasons is that in the list version, we encounter the memory-
copy problem in each iteration of the loop. But in the matrix version, we
encounter it only once. And, of course, the matrix version is vectorized.
But what about usinglapply()on the list version?

>
> set3 <- function(lv) {
+ lv[3] <- 8
+ return(lv)
+}
> z <- list()
> for (i in 1:m) z[[i]] <- sample(1:10,n,replace=T)
> system.time(lapply(z,set3))
user system elapsed
0.100 0.012 0.112

It’s hard to beat vectorized code.

14.4 Using Rprof() to Find Slow Spots in Your Code................................


If you think your R code is running unnecessarily slowly, a handy tool for
finding the culprit isRprof(), which gives you a report of (approximately)
how much time your code is spending in each of the functions it calls. This
is important, as it may not be wise to optimizeeverysection of your program.
Optimization may come at the expense of coding time and code clarity, so
it’s of value to know where optimization would really help.

14.4.1 Monitoring with Rprof()..........................................


Let’s demonstrate usingRprof()with our three versions of code to find a
powers matrix from the previous extended example. We’ll callRprof()to
get the monitor started, run our code, and then callRprof()with a NULL

316 Chapter 14

Free download pdf