[Rcpp-devel] Rcpp speed
Christian Gunning
xian at unm.edu
Wed Dec 22 00:25:56 CET 2010
Two points -
I see inconsistent timings (~10-20% variance between runs) even at
replications = 1e3. Things seem to smooth out around replications =
5e3. Romain's use of 1e4 throughout is a good idea.
Also, += is sugarized and gives a nice example here - Rcpp wins by a
small margin in at least 1 case:
require(Rcpp)
require(inline)
testfun_R <- function(x,y) x+y
code <- '
NumericVector xx(x);
NumericVector yy(y);
NumericVector zz = xx + yy;
return( zz );
'
code1 <- '
IntegerVector xx(x);
IntegerVector yy(y);
yy += xx ;
return( yy );
'
testfun_Rcpp <- cxxfunction(signature(x="integer", y="integer"),
body = code, plugin="Rcpp")
testfun1_Rcpp <- cxxfunction(signature(x="integer", y="integer"),
body = code1, plugin="Rcpp")
testfun_R <- function(x,y) x+y
nreps=5e3
nn <- as.numeric(1:1e2)
ii <- 1L:1e2L
test1 = benchmark(replications = nreps,
R = testfun_R(nn,nn),
Rcpp = testfun_Rcpp(nn, nn),
Rcpp1 = testfun1_Rcpp(nn, nn),
Ri = testfun_R(ii,ii),
Rcppi = testfun_Rcpp(ii, ii),
Rcpp1i = testfun1_Rcpp(ii, ii)
)
nn <- as.numeric(1:1e5)
ii <- 1L:1e5L
test2 = benchmark(replications = nreps,
R = testfun_R(nn,nn),
Rcpp = testfun_Rcpp(nn, nn),
Rcpp1 = testfun1_Rcpp(nn, nn),
Ri = testfun_R(ii,ii),
Rcppi = testfun_Rcpp(ii, ii),
Rcpp1i = testfun1_Rcpp(ii, ii)
)
> test1; test2
test replications elapsed relative user.self sys.self user.child sys.child
1 R 5000 0.043 1.303030 0.044 0.000 0 0
2 Rcpp 5000 1.714 51.939394 1.700 0.016 0 0
3 Rcpp1 5000 1.711 51.848485 1.624 0.080 0 0
6 Rcpp1i 5000 1.689 51.181818 1.664 0.024 0 0
5 Rcppi 5000 1.719 52.090909 1.708 0.012 0 0
4 Ri 5000 0.033 1.000000 0.032 0.000 0 0
test replications elapsed relative user.self sys.self user.child sys.child
1 R 5000 2.842 1.334899 1.440 1.396 0 0
2 Rcpp 5000 10.371 4.871301 8.949 1.404 0 0
3 Rcpp1 5000 8.298 3.897605 8.232 0.048 0 0
6 Rcpp1i 5000 2.129 1.000000 2.108 0.012 0 0
5 Rcppi 5000 16.212 7.614843 11.585 4.513 0 0
4 Ri 5000 3.067 1.440582 2.208 0.852 0 0
best,
Christian
On Tue, Dec 21, 2010 at 8:31 AM,
<rcpp-devel-request at lists.r-forge.r-project.org> wrote:
> For completeness, below is the full script. I also changed funRcpp to use
> IntegerVector.
>
> Dirk
>
>
> require(inline)
> funRcpp <- cxxfunction(signature(x="integer", y="integer"),
> body = 'IntegerVector xx(x);
> IntegerVector yy(y);
> IntegerVector zz = xx + yy;
> return( zz );',
> plugin="Rcpp")
> x <- 1L:3L
> y <- 10L * x
>
> funR <- function(x, y) {
> z <- x + y
> return(z)
> }
>
> stopifnot(identical(funR(x, y), funRcpp(x, y)))
>
> library(rbenchmark)
> x <- 1L:10L
> benchmark(replications = 10000,
> R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
>
> x <- 1L:1000L
> benchmark(replications=1000, R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
>
> x <- 1L:1000000L
> benchmark(replications=100, R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
>
>
--
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
More information about the Rcpp-devel
mailing list