Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve written before about the up- and downsides of the plyr package — I love it’s simplicity, but it can’t be mindlessly applied, no pun intended. This week, I started building a agent-based model for a large population, and I figured I’d use something like a binomial per-timestep birth-death process for between-agent connections.

My ballpark estimate was 1e6 agents using a weekly timestep for about 50 years. This is a stochastic model, so I’d like to replicate it at least 100 times. So, I’ll need at least 50*50*100 = 250,000 steps. I figured I’d be happy if I could get my step runtimes down to ~1 second — dividing the 100 runs over 4 cores, this would give a total runtime of ~17.5 hours. Not short, but not a problem.

At first, I was disheartened to see the runtime of my prototype step function stretching into the minutes. What’s going on? Well, I’d used plyr in a very inappropriate way — for a very large loop. I began to investigate, and discovered that writing an aesthetically unappealing while gave me a 30+x speed-up.

All of which got me thinking — how expensive are loops and function calls in R? Next week I’m leading a tutorial in R and C++ using the wonderful Rcpp and inline packages here at Santa Fe Institute’s 2011 Complex Systems Summer School. Might this make a nice example?

It does, and in spades. Below are the test functions, and you can see that the code complexity increases somewhat from one to the other, but outcomes are identical, again with 30+x speedup for each subsequent case. Here, I’m using the native R API. I also tested using Rcpp to import rbinom(), but that ended up taking twice as long as the naive while loop.

So, the moral of the story seems to be that if you can write a long loop in pure C++, it’s a really easy win.

Note — The as< double >(y); in src below doesn’t seem to copy-and-paste correctly for some reason. If testfun2 doesn’t compile, check to make sure this bit pasted correctly.

### The pure R function definitions

```## use aaply -- the simplest code
require(plyr)
testfun0 <- function(x, y)  aaply(x, 1, function(x) rbinom(1, x, y)

## rewrite the above as an explicit loop
testfun1 = function(nrep, x, y) {
## loop index
ii<-0;
## result vector
ret<-rep(0, nrep);
while(ii < nrep) {
ii<-ii+1;
## how many successes for each element of bb?
ret[ii] <- rbinom(1, x[ii], y)
}
return(ret)
}
```

### Rcpp function definitions (with lots of comments)

```## define source code as string, pass to inline
src <-  '
// Cast input parameters to correct types
// clone prevents direct modification of the input object
IntegerVector tmp(clone(x));
double rate = as< double >(y);
// IntegerVector inherits from STL vector, so we get standard STL methods
int tmpsize = tmp.size();
// initialize RNG from R set.seed() call
RNGScope scope;
// loop
for (int ii =0; ii < tmpsize; ii++) {
// Rf_rbinom is in the R api
// For more details, See Rcpp-quickref.pdf, "Random Functions"
// also, Rcpp provides "safe" accessors via, e.g., tmp(ii)
tmp(ii) = Rf_rbinom(tmp(ii), rate);
};
// tmp points to a native R object, so we can return it as-is
// if we wanted to return, e.g., the double rate, use:
// return wrap(rate)
return tmp;
'

require(inline)
## compile the function, inspect the process with verbose=T
testfun2 = cxxfunction(signature(x='integer', y='numeric'), src, plugin='Rcpp', verbose=T)

## timing function
ps <- function(x) print(system.time(x))
```

### The tests

```## Input vector
bb <- rbinom(1e5, 20, 0.5)
## test each case for 2 different loop lengths
for( nrep in c(1e4, 1e5)){
## initialize RNG each time with the same seed
## plyr
set.seed(20); ps(cc0<- testfun0(nrep[1:nrep], bb, 0.1))
## explicit while loop
set.seed(20); ps(cc1<- testfun1(nrep, bb, 0.1))
## Rcpp
set.seed(20); ps(cc2<- testfun2(bb[1:nrep], 0.1))
print(all.equal(cc1, cc2))
}

## output
user  system elapsed
3.392   0.020   3.417
user  system elapsed
0.116   0.000   0.119
user  system elapsed
0.000   0.000   0.002
[1] TRUE
user  system elapsed
37.534   0.064  37.601
user  system elapsed
1.228   0.000   1.230
user  system elapsed
0.020   0.000   0.021
[1] TRUE
```

### Postlude

After posting this to the very friendly Rcpp-devel mailing list, I got an in-depth reply from Douglas Bates pointing out that, in this case, the performance of vanilla apply() beats the while loop by a narrow margin. He also gives an interesting example of how to use an STL template and an iterator to achieve the same result. I admit that templates are still near-magic to me, and for now I prefer the clarity of the above. Still, if you're curious, this should whet your appetite for some of the wonders of Rcpp.
```## Using Rcpp, inline and STL algorithms and containers

## use the std::binary_function templated struct
inc <-  '
struct Rb : std::binary_function< double, double, double > {
double operator() (double size, double prob) const { return ::Rf_rbinom(size, prob); }
};
'

## define source code as a character vector, pass to inline
src <-  '
NumericVector sz(x);
RNGScope scope;

return NumericVector::import_transform(sz.begin(), sz.end(), std::bind2nd(Rb(), as< double >(y)));
'

## compile the function, inspect the process with verbose=TRUE
f5 <- cxxfunction(signature(x='numeric', y='numeric'), src, 'Rcpp', inc, verbose=TRUE)
```