Skip to content
Prev 944 / 10988 Next

[Rcpp-devel] speed

On Wed, Aug 4, 2010 at 2:27 PM, Sanjog Misra <misra at simon.rochester.edu> wrote:
I get a similar result.

But a more important question is whether your C++ code is producing
the same values are the R function.  In R your sequence is 0:K[i] but
in C++ your code is
Because K is an Integervector, K[i] will be an int and Ki will be an
IntegerVector of length 1.  That is Ki.size() will always be 1 and
cmin0 will be equivalent to the R expression 0:1, not 0:K[i]

There are several other places that the code could be tightened.  For
example, you can avoid the call to the R functions because Rf_dbinom
and Rf_dpois are part of the R API.  Furthermore, the inner loop can
work with scalars instead of vectors if you do the accumulation in a
C++ loop.  Dirk and Romain have done a magnificent job making
different types of R vectors available within C++ but, even so,
operations on C++ native types will always be faster.

But before pursuing that it would be best to straighten out what cmin
and cmin0 should be.  I think they are incorrectly defined.  (Also,
they are loop invariants and their definition, however it is
reconciled, can be moved outside the loop on j.  A smart compiler may
do this but you shouldn't count on that.)