<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Happy new year to everyone, <br>
    <br>
    I have made a very straightforward comparison of the performance of
    standard R, Rcpp function and sugar, and found that the latter
    produces the poorest performance. Let me know what you think and how
    I could improve such performance assessment. <br>
    <br>
    ###################################################<br>
    Summing1 &lt;- cxxfunction(signature(x="numeric"), '<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumericVector xV(x);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double out = sum(xV);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(out);<br>
    ',plugin="Rcpp")<br>
    Summing2 &lt;- cxxfunction(signature(x="numeric"), '<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumericVector xV(x);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double out = 0.0;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;xV.size(); i++) out += xV[i];&nbsp; <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(out);<br>
    ',plugin="Rcpp")<br>
    ###################################################<br>
    # Results.<br>
    n &lt;- 1000000; x &lt;- rnorm(n)<br>
    Summing1(x); Summing2(x); sum(x)<br>
    #######################<br>
    gives: <br>
    [1] -396.6129<br>
    [1] -396.6129<br>
    [1] -396.6129<br>
    <br>
    ###################################################<br>
    # Time.<br>
    system.time(Summing1(x));&nbsp;&nbsp;&nbsp; # Sugar<br>
    system.time(Summing2(x));&nbsp;&nbsp;&nbsp; # Rcpp<br>
    system.time(sum(x));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # R-base<br>
    ###################<br>
    &gt; system.time(Summing1(x));&nbsp; <br>
    &nbsp;&nbsp; user&nbsp; system elapsed <br>
    &nbsp; 0.016&nbsp;&nbsp; 0.000&nbsp;&nbsp; 0.016 <br>
    &gt; system.time(Summing2(x));<br>
    &nbsp;&nbsp; user&nbsp; system elapsed <br>
    &nbsp; 0.008&nbsp;&nbsp; 0.000&nbsp;&nbsp; 0.011 <br>
    &gt; system.time(sum(x));<br>
    &nbsp;&nbsp; user&nbsp; system elapsed <br>
    &nbsp; 0.000&nbsp;&nbsp; 0.000&nbsp;&nbsp; 0.003 <br>
    <br>
    <br>
    Sugar appears to be the slowest! What about Rcpp basic loop? Why
    isn't as fast as the standard sum() in R-base? <br>
    Cheers, <br>
    Cedric <br>
    <br>
    <div class="moz-signature">-- <br>
      <div class="moz-signature"><font face="Times" size="3">
          Cedric Ginestet <br>
          Centre for Neuroimaging Sciences (L3.04) <br>
          NIHR Biomedical Research Centre <br>
          Institute of Psychiatry, Box P089 <br>
          Kings College London <br>
          De Crespigny Park<br>
          London <br>
          SE5 8AF <br>
          <br>
        </font>
      </div>
    </div>
  </body>
</html>