By the time I saw that question yesterday evening, Josh Ulrich had already posted a nice answer suggesting
to switch from ifelse to if to escape the overhead of a vectorised expression on simple scalars. I meekly added a comment
suggesting that Rcpp would likely do well on this and that someone should volunteer
such an answer. Well, it is still August and Mr. Someone is on holiday, so this morning I followed up with such an answer. And as it turns out to
work quite well indeed, we will repost it here.

Let’s start with the general setup, and the two functions supplied by Josh. We also byte-compile these using the compiler package which
is the culmination of several years of work by R core member Luke Tierney. This package was added to R
during the last major release, and we assessed itin this earlier blog post as well as several Rcpp-related
follow-ups. We also load the packages for on-the-fly ‘inline’ compilation and easy benchmarking:

This single R function call takes the code embedded in the argument to the body variable, builds a complete function in temporary file,
and then compiles, links and loads it. We can now call funRcpp() just like the other four functions and do so via thebenchmark() function of the rbenchmark package.

We can focus on the columns labelled elapsed and relative. The C++ version is faster by a factor of almost one-hundred compared to
the byte-compiled version of funtion2, and almost four-hundred times faster than the plain-R variant of function2. And function1 is even worse,
coming at well over twenty-two-hundred times the run-time of the C++ version. Byte-compilation also helps little here.

For comparison, we also ran the original example of a very short vector, called more frequently:

The qualitative ranking is unchanged: the Rcpp version dominates. Function2 usingif instead of the vectorised ifelse is second-best with the byte-compiled version being about twice as fast that the plain R
variant, but still almost three times slower than the C++ version. And the relative differences are less pronounced: relatively speaking, the
function call overhead matters less here and the actual looping matters more: C++ gets a bigger advantage on the actual loop operations in the longer
vectors. That it is an important result as it suggests that on more real-life sized data, the compiled version may reap a larger benefit.

All in all a nice demonstration of Rcpp, and a gain of almost one-hundred to the best
byte-compiled version is nothing to sneeze at—especially when it is so easy to write and load a five-line C++ function thanks toRcpp.

Before closing, a quick reminder that I will giving two classes onRcpp in a few weeks. These will be in New York on September 24, and San Franciso on October 8, seethis blog post as well asthis page at Revolution Analytics (who are a
co-organiser of the classes) for details and registration information.