[Rcpp-devel] Rcpp speed

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 21 16:46:01 CET 2010


On Tue, Dec 21, 2010 at 10:31 AM,  <romain at r-enthusiasts.com> wrote:
> Hello,
>
> I think the most relevant aspect of the time difference is coercion:
>
>> x <- 1:10
>> typeof(x)
> [1] "integer"
>
> So, the constructor of NumericVector has to make a coercion, this is what costs time. As the vector get bigger, the time to coerce them to numeric vectors gets bigger.
>
> The differences are much less dramatic once you do one of these:
> - coerce x to a REALSXP before you get started
> - operate on INTSXP
>
>
> With this code:
>
>
> require(Rcpp)
> require(inline)
> code <- '
> NumericVector xx(x);
> NumericVector yy(y);
> NumericVector zz = xx + yy;
> return( zz );
> '
> testfun_Rcpp <- cxxfunction(signature(x="numeric", y="numeric"),
>    body = code, plugin="Rcpp")
> testfun_R  <- function(x,y) x+y
>
> x <- 1:3
> y <- 10 * x
> testfun_Rcpp(x, y)
>
> library(rbenchmark)
> x <- as.numeric(1:10)
> benchmark(replications = 10000,
>    R = testfun_R(x,x),
>    Rcpp = testfun_Rcpp(x, x)
>    )
>
> x <- as.numeric(1:1000)
> benchmark(replications = 10000,
>    R = testfun_R(x,x),
>    Rcpp = testfun_Rcpp(x, x)
> )
>
> x <- as.numeric(1:1000000)
> benchmark(replications = 10000,
>    R = testfun_R(x,x),
>    Rcpp = testfun_Rcpp(x, x)
> )
>
>
> I get these results:
>
>  test replications elapsed relative user.self sys.self user.child sys.child
> 1    R        10000   0.050     1.00     0.049    0.001          0         0
> 2 Rcpp        10000   0.067     1.34     0.065    0.002          0         0
>  test replications elapsed relative user.self sys.self user.child sys.child
> 1    R        10000   0.087 1.000000     0.083    0.004          0         0
> 2 Rcpp        10000   0.145 1.666667     0.145    0.000          0         0
>  test replications elapsed relative user.self sys.self user.child sys.child
> 1    R        10000  49.189 1.000000    35.405   13.776          0         0
> 2 Rcpp        10000 100.849 2.050235    87.029   13.814          0         0
>
>
> However, it is still true that the bigger x is, the bigger the ratio Rcpp / R gets. Not sure why. Sounds like a nice xmas activity.
>
> It might be worth looping internally for the benchmark to dilute some of the confusion factor that might be related to inline/rbenchmark.
>

Thanks. That and Dirk's last post do seem to explain a large part of
the difference.

I agree it would still be a good idea to try to understand why the
ratio increases even when we eliminate the coercion and if there is
anything that can be done about it.

I am sure my benchmark is a bit lame although it did bring out some
issues nevertheless.  It would be a good idea to generate some
benchmarks USING SUGAR to see what kind of speed ups can be achieved.
Since there is a degree of effort in getting into all this I sure
would like to see benchmarks that show it will likely pay off in
improved performance before spending too much time on this.

P.S. In Dirk's post we don't need the L's in the seq expressions since
m:n is integer anyways in those cases; however, in y <- 10 * x we do
need 10L.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com


More information about the Rcpp-devel mailing list