title: Why a self-written Rcpp vectorized mathematical function is faster than its base counterpart?
body: <p>OK, I know the answer, but being inspired by <a href="https://stackoverflow.com/questions/26430571">this question</a>, I'd like to get some nice opinions about the following: Why the Rcpp exercise below is ca. 15% faster (for long vectors) than the built-in <code>exp()</code>? We all know that Rcpp is a wrapper to the R/C API, so we should expect a slightly worse performance.</p>
  
  <pre><code>Rcpp::cppFunction("
     NumericVector exp2(NumericVector x) {
        NumericVector z = Rcpp::clone(x);
        int n = z.size();
        for (int i=0; i&lt;n; ++i)
           z[i] = exp(z[i]);
        return z;
     }
  ")
  
  library("microbenchmark")
  x &lt;- rcauchy(1000000)
  microbenchmark(exp(x), exp2(x), unit="relative")
  ## Unit: relative
  ##     expr      min       lq   median       uq      max neval
  ##   exp(x) 1.159893 1.154143 1.155856 1.154482 0.926272   100
  ##  exp2(x) 1.000000 1.000000 1.000000 1.000000 1.000000   100
  </code></pre>