[Rcpp-devel] sugar performance
romain at r-enthusiasts.com
romain at r-enthusiasts.com
Wed Dec 22 12:33:41 CET 2010
Hello,
Following Gabor's thread yesterday, I looked into R source code and figure that we don't need to test for NA when using numeric vector. See this:
require(inline)
require(Rcpp)
pl <- cxxfunction( , '
return List::create(
NA_REAL + NA_REAL,
NA_REAL + 1.0,
1.0 + NA_REAL
);
', plugin = "Rcpp" )
pl()
As a consequence, I've updated plus.h to skip the tests when dealing with numeric sugar expressions.
Also, I'm trying to write the looping internally in order to get a better comparison. With the attached file, I get:
romain@ /tmp $ Rscript gab.R
Le chargement a nécessité le package : inline
Le chargement a nécessité le package : methods
Le chargement a nécessité le package : Rcpp
Le chargement a nécessité le package : rbenchmark
test replications elapsed relative user.self sys.self user.child sys.child
1 sugar 1 0.002 1.0 0.001 0 0 0
2 R 1 0.005 2.5 0.004 0 0 0
test replications elapsed relative user.self sys.self user.child sys.child
1 sugar 1 0.035 1.0 0.034 0.000 0 0
2 R 1 0.049 1.4 0.049 0.001 0 0
test replications elapsed relative user.self sys.self user.child sys.child
1 sugar 1 4.699 1.000000 3.701 0.999 0 0
2 R 1 5.324 1.133007 4.267 1.056 0 0
test replications elapsed relative user.self sys.self user.child sys.child
2 R 1 45.995 1.000000 39.076 6.911 0 0
1 sugar 1 61.249 1.331645 46.178 15.067 0 0
Another contributor to the time difference is the handling of garbage collection. Each time the constructor of an Rcpp class is called, a call to R_PreserveObject is issued. And each time the destructor is called, R_ReleaseObject is called. In this R-devel thread last year:
http://thread.gmane.org/gmane.comp.lang.r.devel/23167 , some inefficiency of these functions were revealed, with a patch from Simon, but this has not been incorporated into R yet.
Maybe we can send a reminder to sponsor the patch or use something else in Rcpp instead.
The benchmark shows that sugar beats R first, and then R gets better as the size of the vectors grow. I think this is related to loop unrolling. it seems that it gives good performance for smaller lengths and not as good for bigger lengths.
Romain
More information about the Rcpp-devel
mailing list