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 <- cxxfunction(signature(x="numeric"), '
      NumericVector xV(x);
      double out = sum(xV);
      return wrap(out);
',plugin="Rcpp")
Summing2 <- cxxfunction(signature(x="numeric"), '
      NumericVector xV(x);
      double out = 0.0;
      for(int i=0; i<xV.size(); i++) out += xV[i];
      return wrap(out);
',plugin="Rcpp")
###################################################
# Results.
n <- 1000000; x <- 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
###################
> system.time(Summing1(x));
   user  system elapsed
  0.016   0.000   0.016
> system.time(Summing2(x));
   user  system elapsed
  0.008   0.000   0.011
> 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?
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
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to