[Rcpp-devel] Dirk's benchmarking of code for creation of random binary matrices

Douglas Bates bates at stat.wisc.edu
Tue Sep 4 19:22:14 CEST 2012


Slightly different results if I clean up the eigenFloor version and
use unif_rand() instead of runif.  The sugar version is still faster,
however.
-------------- next part --------------

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(inline)
> library(compiler)
> library(rbenchmark)
> 
> n <- 500
> k <- 100
> scott <- function(N, K) {
+     mm <- matrix(0, N, K)
+     apply(mm, c(1, 2), function(x) sample(c(0, 1), 1))
+ }
> scottComp <- cmpfun(scott)
> ted <- function(N, K) {
+     matrix(rbinom(N * K, 1, 0.5), ncol = K, nrow = N)
+ }
> david <- function(m, n) {
+     matrix(sample(0:1, m * n, replace = TRUE), m, n)
+ }
> luis <- function(m, n) {
+      round(matrix(runif(m * n), m, n))
+ }
> armaFloor <- cxxfunction(signature(ns="integer", ks="integer"), plugin = "RcppArmadillo", body='
+    int n = Rcpp::as<int>(ns);
+    int k = Rcpp::as<int>(ks);
+    return wrap(arma::floor(arma::randu(n, k) + 0.5));
+ ')
> sugarFloor <- cxxfunction(signature(ns="integer", ks="integer"), plugin = "Rcpp", body='
+    int n = Rcpp::as<int>(ns);
+    int k = Rcpp::as<int>(ks);
+    Rcpp::RNGScope tmp;
+    Rcpp::NumericVector draws = Rcpp::floor(Rcpp::runif(n*k)+0.5);
+    return Rcpp::NumericMatrix(n, k, draws.begin());
+ ')
> eigenFloor <- cxxfunction(signature(ns="integer", ks="integer"), plugin = "RcppEigen", body='
+    int n(Rf_asInteger(ns)), k(Rf_asInteger(ks));
+    Rcpp::RNGScope tmp;
+    return Rcpp::wrap(Eigen::ArrayXXd(n, k).unaryExpr(std::ptr_fun(rbinary)));
+ ', includes='
+    inline double rbinary(double x) {return std::floor(unif_rand() + 0.5);}
+ ')
Loading required package: lattice

Attaching package: ?RcppEigen?

The following object(s) are masked from ?package:RcppArmadillo?:

    fastLm, fastLmPure

> res <- benchmark(scott(n, k), scottComp(n,k),
+                  ted(n, k), david(n, k), luis(n, k),
+                  armaFloor(n, k), sugarFloor(n, k),
+                  eigenFloor(n, k),
+                  order="relative", replications=100)
> print(res[,1:4])
              test replications elapsed   relative
6  armaFloor(n, k)          100   0.145   1.000000
7 sugarFloor(n, k)          100   0.161   1.110345
8 eigenFloor(n, k)          100   0.189   1.303448
4      david(n, k)          100   0.219   1.510345
3        ted(n, k)          100   0.571   3.937931
5       luis(n, k)          100   0.755   5.206897
2  scottComp(n, k)          100  45.467 313.565517
1      scott(n, k)          100  45.804 315.889655
> 
> proc.time()
   user  system elapsed 
109.610   1.048 110.839 


More information about the Rcpp-devel mailing list