<html><body><div style="color:#000; background-color:#fff; font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:8pt"><div><span style="font-size: 10px;">The next test compares the
speeds:</span></div><div><span style="font-size: x-small;"><br clear="none"></span></div><div>
<div><span style="font-size: x-small;">// [[Rcpp::export]]</span></div>
<div><span style="font-size: x-small;">NumericVector rowApply0(NumericMatrix&
x, const Function& FUN)</span></div>
<div><span style="font-size: x-small;">{</span></div>
<div><span style="font-size: x-small;"> int n = x.nrow();</span></div>
<div><span style="font-size: x-small;"> NumericVector result =
no_init(n);</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;"> for (int r = 0; r < n; r++)
{</span></div>
<div><span style="font-size: x-small;"> result[r] = as<double>(FUN(x(r,
_) ) );</span></div>
<div><span style="font-size: x-small;"> }</span></div>
<div><span style="font-size: x-small;"> return result;</span></div>
<div><span style="font-size: x-small;">}</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">// [[Rcpp::export]]<br clear="none"></span></div>
<div><span style="font-size: x-small;">NumericVector rowApply1(NumericMatrix&
x, const Function& FUN)</span></div>
<div><span style="font-size: x-small;">{</span></div>
<div><span style="font-size: x-small;"> int n = x.nrow();</span></div>
<div><span style="font-size: x-small;"> NumericVector result =
no_init(n);</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;"> for (int r = 0; r < n; r++)
{</span></div>
<div><span style="font-size: x-small;"> Language call(FUN, x(r, _))
;</span></div>
<div><span style="font-size: x-small;"> result[r] =
as<double>(call.fast_eval() );</span></div>
<div><span style="font-size: x-small;"> }</span></div>
<div><span style="font-size: x-small;"> return result;</span></div>
<div><span style="font-size: x-small;">}</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">// [[Rcpp::export]]</span></div>
<div><span style="font-size: x-small;">NumericVector rowApply2(NumericMatrix&
x, const Function& FUN)</span></div>
<div><span style="font-size: x-small;">{</span></div>
<div><span style="font-size: x-small;"> int n = x.nrow();</span></div>
<div><span style="font-size: x-small;"> NumericVector result =
no_init(n);</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;"> Language call(FUN,
R_NilValue);</span></div>
<div><span style="font-size: x-small;"> Language::Proxy proxy(call,
1);</span></div>
<div><span style="font-size: x-small;"> for (int r = 0; r < n; r++)
{</span></div>
<div><span style="font-size: x-small;"> proxy = x(r, _) ;</span></div>
<div><span style="font-size: x-small;"> result[r] =
as<double>(call.fast_eval() );</span></div>
<div><span style="font-size: x-small;"> }</span></div>
<div><span style="font-size: x-small;"> return result;</span></div>
<div><span style="font-size: x-small;">}</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div></div><div>
<div><span style="font-size: x-small;">> M <- matrix(rnorm(15L),
nrow=3L);</span></div>
<div><span style="font-size: x-small;">> identical(rowMeans(M), apply(M, 1L,
mean));</span></div>
<div><span style="font-size: x-small;">[1] TRUE</span></div>
<div><span style="font-size: x-small;">> identical(rowMeans(M), rowApply0(M,
mean));</span></div>
<div><span style="font-size: x-small;">[1] TRUE</span></div>
<div><span style="font-size: x-small;">> identical(rowMeans(M), rowApply1(M,
mean));</span></div>
<div><span style="font-size: x-small;">[1] TRUE</span></div>
<div><span style="font-size: x-small;">> identical(rowMeans(M), rowApply2(M,
mean));</span></div>
<div><span style="font-size: x-small;">[1] TRUE</span></div>
<div><span style="font-size: x-small;">> benchmark(rowMeans(M),</span></div>
<div><span style="font-size: x-small;">+ apply(M, 1L,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply0(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply1(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply2(M,
mean),</span></div>
<div><span style="font-size: x-small;">+
replications=300000L);</span></div>
<div><span style="font-size: x-small;"> test replications elapsed
relative user.self sys.self user.child sys.child</span></div>
<div><span style="font-size: x-small;">3 rowApply0(M, mean) 300000 23.26
8.551 23.14 0 NA NA</span></div>
<div><span style="font-size: x-small;">2 apply(M, 1, mean) 300000 21.30
7.831 21.13 0 NA NA</span></div>
<div><span style="font-size: x-small;">4 rowApply1(M, mean) 300000 10.23
3.761 10.22 0 NA NA</span></div>
<div><span style="font-size: x-small;">5 rowApply2(M, mean) 300000 9.87
3.629 9.82 0 NA NA</span></div>
<div><span style="font-size: x-small;">1 rowMeans(M) 300000 2.72
1.000 2.72 0 NA NA</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">> M <- matrix(rnorm(1500L),
nrow=30L);</span></div>
<div><span style="font-size: x-small;">> benchmark(rowMeans(M),</span></div>
<div><span style="font-size: x-small;">+ apply(M, 1L,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply0(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply1(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply2(M,
mean),</span></div>
<div><span style="font-size: x-small;">+
replications=30000L);</span></div>
<div><span style="font-size: x-small;"> test replications elapsed
relative user.self sys.self user.child sys.child</span></div>
<div><span style="font-size: x-small;">3 rowApply0(M, mean) 30000 21.32
48.455 21.30 0 NA NA</span></div>
<div><span style="font-size: x-small;">2 apply(M, 1, mean) 30000 11.17
25.386 11.17 0 NA NA</span></div>
<div><span style="font-size: x-small;">4 rowApply1(M, mean) 30000 8.24
18.727 8.19 0 NA NA</span></div>
<div><span style="font-size: x-small;">5 rowApply2(M, mean) 30000 8.10
18.409 8.08 0 NA NA</span></div>
<div><span style="font-size: x-small;">1 rowMeans(M) 30000 0.44
1.000 0.43 0 NA NA</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">> M <- matrix(rnorm(150000L),
nrow=300L);</span></div>
<div><span style="font-size: x-small;">> benchmark(rowMeans(M),</span></div>
<div><span style="font-size: x-small;">+ apply(M, 1L,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply0(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply1(M,
mean),</span></div>
<div><span style="font-size: x-small;">+ rowApply2(M,
mean),</span></div>
<div><span style="font-size: x-small;">+
replications=3000L);</span></div>
<div><span style="font-size: x-small;"> test replications elapsed
relative user.self sys.self user.child sys.child</span></div>
<div><span style="font-size: x-small;">3 rowApply0(M, mean) 3000 26.65
18.379 26.41 0.01 NA NA</span></div>
<div><span style="font-size: x-small;">2 apply(M, 1, mean) 3000 19.76
13.628 19.71 0.00 NA NA</span></div>
<div><span style="font-size: x-small;">4 rowApply1(M, mean) 3000 12.79
8.821 12.78 0.00 NA NA</span></div>
<div><span style="font-size: x-small;">5 rowApply2(M, mean) 3000 12.69
8.752 12.68 0.00 NA NA</span></div>
<div><span style="font-size: x-small;">1 rowMeans(M) 3000 1.45
1.000 1.45 0.00 NA NA</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">again, as Romain suggested, implementation
rowApply2 is faster than rowApply1, and rowApply0 (which triggers tryCatch) is
even slower than R's apply.</span></div>
<div><span style="font-size: x-small;"><br clear="none"></span></div>
<div><span style="font-size: x-small;">Thanks Romain !</span></div></div><div>
</div><div style="font-size: 8pt;"><br clear="none"></div></div></body></html>