[Rd] Proposed speedup of ifelse
Hugh Parsonage
hugh@p@r@on@ge @ending from gm@il@com
Fri May 4 06:58:37 CEST 2018
Thanks Radford. I concur with all your points. I've attempted to address
the issues you raised through the github.io post. The new method appears
to be slower for test lengths < 100 and possibly longer lengths (not just <
10). Of course length(test) < 100 is very quick, so I simply added this to
the conditions that cause the old ifelse method to be invoked. I'll leave
it to R-core to decide whether or not the benefits for longer vectors are
worth it.
On Fri, 4 May 2018 at 01:01 Radford Neal <radford at cs.toronto.edu> wrote:
> > 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
>
[[alternative HTML version deleted]]
More information about the R-devel
mailing list