4 Implementations

4.1 Six Versions

We have implemented the APL functions in six different ways

1. As pure R.
2. As R, with only decode and encode in C using .C().
3. As C, using the .C() interface for decode, encode, transpose, select, reduce, scan, and inner product.
4. As C, using the .Call() interface for decode, encode, transpose, select, reduce, scan, and inner product.
5. As C, using the .Call() interface, for transpose, select, reduce, scan, and inner product, with decode and encode inlined using .C().
6. As C using Rcpp for decode, encode, transpose, select, reduce, scan, and inner product.

It must be emphasized that the Rcpp interface was written five years ago, with a very early version of Rcpp, that uses only a tiny subset of the possibilities offered by newer versions. We are sure a great deal of improvement is possible there.

Also for reduce, scan, and inner product some of our arguments are functions. In the .C() implementation we use the old Call_R() interface, dating back to the Blue Book (Becker, Chambers, and Wilks (1988)), to handle function pointers.

Note that inlining decode and encode makes sense, since they are called so many times in all functions, but the inline keyword is handled only as a hint to the compiler. It does not guarantee actual inlining of the function.

The computations in the body of the paper use the Call() interface. The different implementations calling C routines use different shared libraries and different glue routines in R.

4.2 Timing

We compare running time of the six implementations. The parameters we use are

repNum <- 10
timeArr <- array(NA, c(repNum,3,3,6))
a<-array(1:10000,c(10,10,100))
b<-array(1:10000,c(100,10,10))
c<-array(1:100000,rep(10,5))
d<-array(1:100000,rep(10,5))
x<-list(1:5,1:5,1:5,1:5,1:5)

and the results are

## , , InnerProduct
##
##              R    R+.C     .C  .Call Inline   Rcpp
## user    9.8618 10.6167 0.8599 0.3539 0.2350 0.7223
## system  0.0319  0.0249 0.0396 0.0056 0.0048 0.0064
## elapsed 9.9280 10.6655 0.9019 0.3611 0.2407 0.7300
##
## , , Reduce
##
##              R   R+.C     .C  .Call Inline   Rcpp
## user    1.7059 1.0108 0.0463 0.0464 0.0315 0.0699
## system  0.0061 0.0022 0.0051 0.0014 0.0008 0.0011
## elapsed 1.7177 1.0149 0.0515 0.0478 0.0324 0.0710
##
## , , Select
##
##              R   R+.C    .C .Call Inline   Rcpp
## user    0.0642 0.0422 7e-04 9e-04  4e-04 0.0023
## system  0.0002 0.0001 0e+00 0e+00  0e+00 0.0000
## elapsed 0.0649 0.0425 2e-04 9e-04  7e-04 0.0020