[Rd] Proposed speedup of ifelse

Radford Neal r@dford @ending from c@@toronto@edu
Thu May 3 17:01:52 CEST 2018


> I propose a patch to ifelse that leverages anyNA(test) to achieve an
> improvement in performance. For a test vector of length 10, the change
> nearly halves the time taken and for a test of length 1 million, there
> is a tenfold increase in speed. Even for small vectors, the
> distributions of timings between the old and the proposed ifelse do
> not intersect.

For smaller vectors, your results are significantly affected by your
invoking the old version via base::ifelse.  You could try defining
your new version as new_ifelse, and invoking the old version as just
ifelse.  There might still be some issues with the two versions having
different context w.r.t environments, and hence looking up functions
in different ways.  You could copy the code of the old version and
define it in the global environment just like new_ifelse.

When using ifelse rather than base::ifelse, it seems the new version
is slower for vectors of length 10, but faster for long vectors.

Also, I'd use system.time rather than microbenchmark.  The latter will
mix invocations of the two functions in a way where it is unclear that
garbage collection time will be fairly attributed.  Also, it's a bit
silly to plot the distributions of times, which will mostly reflect
variations in when garbage collections at various levels occur - just
the mean is what is relevant.

Regards,

   Radford Neal




More information about the R-devel mailing list