<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Dear All, <br>
    <br>
    Here are some simulations that I have run this morning. Romain's
    suggestion to compute xV.size() before the loop and Douglas' idea of
    using accumulate appear to work best. However, both are
    substantially slower than the r-base function. <br>
    <br>
    I have also included two more versions: (i) one similar to Romain's
    but using pre-incrementation<b> </b>in the loop and (ii) one using
    the iterator in the loop. Another option may be to use the C++ boost
    library. I don't know if anyone on this list has experience with
    using boost.<br>
    <br>
    See the results of the simulations below (N=1000 data sets). <br>
    Ced<br>
    <br>
#####################################################################<br>
    ## Functions.<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>
    Summing3 &lt;- cxxfunction(signature(x="numeric"), '<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumericVector xV(x);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double out = 0.0; int N=xV.size();<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;N; i++) out += xV[i];&nbsp; <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(out);<br>
    ',plugin="Rcpp")<br>
    Summing4 &lt;- cxxfunction(signature(x="numeric"), '<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumericVector xV(x);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(std::accumulate(xV.begin(), xV.end(), double()));<br>
    ',plugin="Rcpp")<br>
    Summing5 &lt;- cxxfunction(signature(x="numeric"), '<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumericVector xV(x);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double out = 0.0; int N=xV.size();<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=0; i&lt;N; ++i) out += xV[i];&nbsp; <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(out);<br>
    ',plugin="Rcpp")<br>
    Summing6 &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(NumericVector::iterator i=xV.begin(); i!=xV.end(); ++i)
    out += *i;&nbsp; <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrap(out);<br>
    ',plugin="Rcpp")<br>
    <br>
#####################################################################<br>
    ## Simulation: Time Testing. <br>
    n &lt;- 1000000; N &lt;- 1000<br>
    time.Sum &lt;- matrix(0,N,7);<br>
    for(i in 1:N){<br>
    x &lt;- rnorm(n)<br>
    time.Sum[i,1] &lt;- system.time(Summing1(x))[3];&nbsp; <br>
    time.Sum[i,2] &lt;- system.time(Summing2(x))[3];<br>
    time.Sum[i,3] &lt;- system.time(Summing3(x))[3];&nbsp; <br>
    time.Sum[i,4] &lt;- system.time(Summing4(x))[3];<br>
    time.Sum[i,5] &lt;- system.time(Summing5(x))[3];<br>
    time.Sum[i,6] &lt;- system.time(Summing6(x))[3];<br>
    time.Sum[i,7] &lt;- system.time(sum(x))[3];&nbsp; <br>
    }# i <br>
    time.df &lt;- data.frame(time.Sum)<br>
    names(time.df) &lt;-
    c("Sugar","Rcpp","Rcpp_N","Accumulate","Pre-increment","Iterator","R")<br>
    boxplot(time.df)<br>
    <br>
#####################################################################<br>
    ## RESULTS:<br>
    formatC(summary(time.df),dec=3)<br>
    &nbsp;&nbsp;&nbsp;&nbsp; Sugar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Rcpp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Rcpp_N&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
    &nbsp;" Min.&nbsp;&nbsp; :0.01600&nbsp; " " Min.&nbsp;&nbsp; :0.01000&nbsp; " "Min.&nbsp;&nbsp; :0.005000&nbsp; "<br>
    &nbsp;" 1st Qu.:0.01600&nbsp; " " 1st Qu.:0.01000&nbsp; " "1st Qu.:0.005000&nbsp; "<br>
    &nbsp;" Median :0.01600&nbsp; " " Median :0.01100&nbsp; " "Median :0.006000&nbsp; "<br>
    &nbsp;" Mean&nbsp;&nbsp; :0.01631&nbsp; " " Mean&nbsp;&nbsp; :0.01060&nbsp; " "Mean&nbsp;&nbsp; :0.005668&nbsp; "<br>
    &nbsp;" 3rd Qu.:0.01600&nbsp; " " 3rd Qu.:0.01100&nbsp; " "3rd Qu.:0.006000&nbsp; "<br>
    &nbsp;" Max.&nbsp;&nbsp; :0.03700&nbsp; " " Max.&nbsp;&nbsp; :0.02400&nbsp; " "Max.&nbsp;&nbsp; :0.020000&nbsp; "<br>
    &nbsp;&nbsp; Accumulate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pre-increment&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Iterator&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
    &nbsp;"Min.&nbsp;&nbsp; :0.005000&nbsp; " "Min.&nbsp;&nbsp; :0.005000&nbsp; " " Min.&nbsp;&nbsp; :0.01000&nbsp; "<br>
    &nbsp;"1st Qu.:0.005000&nbsp; " "1st Qu.:0.005000&nbsp; " " 1st Qu.:0.01000&nbsp; "<br>
    &nbsp;"Median :0.006000&nbsp; " "Median :0.006000&nbsp; " " Median :0.01100&nbsp; "<br>
    &nbsp;"Mean&nbsp;&nbsp; :0.005714&nbsp; " "Mean&nbsp;&nbsp; :0.005697&nbsp; " " Mean&nbsp;&nbsp; :0.01065&nbsp; "<br>
    &nbsp;"3rd Qu.:0.006000&nbsp; " "3rd Qu.:0.006000&nbsp; " " 3rd Qu.:0.01100&nbsp; "<br>
    &nbsp;"Max.&nbsp;&nbsp; :0.029000&nbsp; " "Max.&nbsp;&nbsp; :0.021000&nbsp; " " Max.&nbsp;&nbsp; :0.03100&nbsp; "<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; R&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
    &nbsp;"Min.&nbsp;&nbsp; :0.002000&nbsp; "<br>
    &nbsp;"1st Qu.:0.002000&nbsp; "<br>
    &nbsp;"Median :0.002000&nbsp; "<br>
    &nbsp;"Mean&nbsp;&nbsp; :0.002211&nbsp; "<br>
    &nbsp;"3rd Qu.:0.002000&nbsp; "<br>
    &nbsp;"Max.&nbsp;&nbsp; :0.004000&nbsp; "<br>
#####################################################################<br>
    <br>
    PS: Apologies to Dirk as I have not followed his advice, yet. <br>
    <br>
    -- <br>
    <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>
    </font><br>
    <br>
    On 04/01/11 15:37, Dirk Eddelbuettel wrote:
    <blockquote cite="mid:19747.16030.286930.862635@max.nulle.part"
      type="cite">
      <pre wrap="">
On 4 January 2011 at 15:14, Cedric Ginestet wrote:
| Happy new year to everyone,

| 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.

| ###################################################
| Summing1 &lt;- cxxfunction(signature(x="numeric"), '
|       NumericVector xV(x);
|       double out = sum(xV);
|       return wrap(out);
| ',plugin="Rcpp")
| Summing2 &lt;- cxxfunction(signature(x="numeric"), '
|       NumericVector xV(x);
|       double out = 0.0;
|       for(int i=0; i&lt;xV.size(); i++) out += xV[i]; 
|       return wrap(out);
| ',plugin="Rcpp")
| ###################################################
| # Results.
| n &lt;- 1000000; x &lt;- rnorm(n)
| Summing1(x); Summing2(x); sum(x)
| #######################
| gives:
| [1] -396.6129
| [1] -396.6129
| [1] -396.6129

| ###################################################
| # Time.
| system.time(Summing1(x));    # Sugar
| system.time(Summing2(x));    # Rcpp
| system.time(sum(x));               # R-base
| ###################
| &gt; system.time(Summing1(x)); 
|    user  system elapsed
|   0.016   0.000   0.016
| &gt; system.time(Summing2(x));
|    user  system elapsed
|   0.008   0.000   0.011
| &gt; system.time(sum(x));
|    user  system elapsed
|   0.000   0.000   0.003


| Sugar appears to be the slowest! What about Rcpp basic loop? Why isn't as fast
| as the standard sum() in R-base?

1)  Try to think a about measurement error here; these times are all minuscule.

2)  Consider reading the list archive, we have better use of benchmarks using
    rbenchmark and replications; these are also some example in the examples
    right in Rcpp

3)  Consider reading the list archive and discussions about the NoNA tests.

4)  Lastly, consider Romain's point about a baseline using an empty function. 

Dirk 


| Cheers,
| Cedric

| --
| Cedric Ginestet
| Centre for Neuroimaging Sciences (L3.04)
| NIHR Biomedical Research Centre
| Institute of Psychiatry, Box P089
| Kings College London
| De Crespigny Park
| London
| SE5 8AF


| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| <a class="moz-txt-link-abbreviated" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a>
| <a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a>

</pre>
    </blockquote>
  </body>
</html>