Dirk Eddelbuettel — written Jan 7, 2013 — source
Since the 0.10.* release series, Rcpp contains a new sugar function clamp
which can be used to limit vectors to both a minimum and maximim value.
This recent StackOverflow question permitted clamp
to
shine. We retake some of the answers, including the clamp
entry by Romain.
We first define the three R versions.
We then define some data, and ensure that these versions all producing identical results.
Next is the C++ solution: a one-liner thanks to the existing sugar function.
We can then check and benchmark the new C++ version.
test replications elapsed relative 4 rcppClamp(x, a, b) 100 0.119 1.000 3 operationsClamp(x, a, b) 100 0.505 4.244 1 pminpmaxClamp(x, a, b) 100 0.530 4.454 2 ifelseClamp(x, a, b) 100 5.268 44.269
We see a decent gain of the Rcpp version even relative to these
vectorised R solutions. Among these, the simplest (based on
ifelse
) is by far the slowest. The parallel min/max version is
about as faster as the clever-but-less-readable expression-based
solution.
Real “production” solutions will of course need some more testing
of inputs etc. However, as an illustration of clamp
this example
has hopefully been compelling.