[Rcpp-devel] Rcpp speed

Dirk Eddelbuettel edd at debian.org
Tue Dec 21 16:29:44 CET 2010


On 21 December 2010 at 09:24, Dirk Eddelbuettel wrote:
| 
| Romain rightly scolds me once again for me having forgotten about the fact
| that INTSXP != REALSXP, and conversion to NumericVector has a cost.  So once
| we keep everything in int, there is no difference:
| 
| R> x <- 1L:10L
| R> benchmark(replications = 10000,
| +           R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
|   test replications elapsed relative user.self sys.self user.child sys.child
| 1    R        10000   0.041   1.0000      0.04        0          0         0
| 3 Rcpp        10000   0.054   1.3171      0.05        0          0         0
| 2 Rfun        10000   0.054   1.3171      0.06        0          0         0
| R> x <- 1L:1000L
| R> benchmark(replications=1000, R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
|   test replications elapsed relative user.self sys.self user.child sys.child
| 1    R         1000   0.008   1.3333      0.01        0          0         0
| 3 Rcpp         1000   0.006   1.0000      0.01        0          0         0
| 2 Rfun         1000   0.009   1.5000      0.01        0          0         0
| R> x <- 1L:1000000L
| R> benchmark(replications=100, R = x + x, Rfun = funR(x, x), Rcpp = funRcpp(x, x))
|   test replications elapsed relative user.self sys.self user.child sys.child
| 1    R          100   0.436   1.5034      0.43     0.00          0         0
| 3 Rcpp          100   0.290   1.0000      0.18     0.11          0         0
| 2 Rfun          100   0.461   1.5897      0.44     0.01          0         0
| R> 

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))


-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list