On 21 December 2010 at 09:15, Gabor Grothendieck wrote: | Can anyone explain these results? Rcpp takes 30% more time than R | with 10 elements, the same time as R with 1000 elements and 7x as long | with a million elements. I would have expected the ratio to be | highest for `10 elements since that would be dominated by the call | sequence but its the other way around and it seems to get relatively | less efficient as the size of the vector grows.
Errr, these are _single expressions in the R parser_ ! So how could they be faster in Rcpp when we have to do the extra marshalling of objects on the heap, NA tests, exception handlings, etc pp ? Did you seriously thing there could be a gain? If you have a real task, time that. Do you recall the issue started by Radford Neal about curly or straight paranthese? When Christian Robert followed up and that whole thing became a little blogging storm, I wrote an ad-hoc post ... demonstrating an 80-fold speed increase with Rcpp. That was in September, and be we may well have got faster since as a number of tweaks went into Rcpp. In any event, the aforementioned blog post is http://dirk.eddelbuettel.com/blog/2010/09/07#straight_curly_or_compiled Otherwise, thanks for being empirical and posting replicable code using inline and rbenchmark. That is the right idea. But I fear you started to measure the wrong question here. Cheers, Dirk | | > require(inline) | > testfun <- cxxfunction(signature(x="numeric", y="numeric"), | + body = ' | + NumericVector xx(x); | + NumericVector yy(y); | + NumericVector zz = xx + yy; | + return( zz ); | + ', plugin="Rcpp") | > x <- 1:3 | > y <- 10 * x | > testfun(x, y) | [1] 11 22 33 | > | > library(rbenchmark) | > x <- 1:10 | > benchmark(replications = 10000, R = x + x, Rcpp = testfun(x, x)) | test replications elapsed relative user.self sys.self user.child sys.child | 1 R 10000 0.17 1.000000 0.16 0 NA NA | 2 Rcpp 10000 0.22 1.294118 0.23 0 NA NA | > | > x <- 1:1000 | > benchmark(R = x + x, Rcpp = testfun(x, x)) | test replications elapsed relative user.self sys.self user.child sys.child | 1 R 100 0.00 0 0.00 0 NA NA | 2 Rcpp 100 0.01 1 0.01 0 NA NA | > | > x <- 1:1000000 | > benchmark(R = x + x, Rcpp = testfun(x, x)) | test replications elapsed relative user.self sys.self user.child sys.child | 1 R 100 2.34 1.000000 1.81 0.15 NA NA | 2 Rcpp 100 16.64 7.111111 12.63 1.66 NA NA | | | -- | Statistics & Software Consulting | GKX Group, GKX Associates Inc. | tel: 1-877-GKX-GROUP | email: ggrothendieck at gmail.com | _______________________________________________ | Rcpp-devel mailing list | [email protected] | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel -- Dirk Eddelbuettel | [email protected] | http://dirk.eddelbuettel.com _______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
