4 Implementations
4.1 Six Versions
We have implemented the APL functions in six different ways
- As pure R.
- As R, with only decode and encode in C using
.C()
. - As C, using the
.C()
interface for decode, encode, transpose, select, reduce, scan, and inner product. - As C, using the
.Call()
interface for decode, encode, transpose, select, reduce, scan, and inner product. - As C, using the
.Call()
interface, for transpose, select, reduce, scan, and inner product, with decode and encode inlined using.C()
. - 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.7747 10.9567 0.8966 0.3493 0.2352 0.7138
## system 0.0449 0.0431 0.0437 0.0042 0.0042 0.0117
## elapsed 9.9147 11.0559 0.9480 0.3547 0.2408 0.7277
##
## , , Reduce
##
## R R+.C .C .Call Inline Rcpp
## user 1.6620 1.0013 0.0474 0.0469 0.0321 0.0651
## system 0.0066 0.0030 0.0055 0.0014 0.0003 0.0018
## elapsed 1.6780 1.0076 0.0536 0.0485 0.0328 0.0670
##
## , , Select
##
## R R+.C .C .Call Inline Rcpp
## user 0.0641 0.0453 6e-04 0.0011 7e-04 0.0021
## system 0.0004 0.0000 0e+00 0.0000 0e+00 0.0000
## elapsed 0.0648 0.0452 6e-04 0.0010 3e-04 0.0021