From r@dford @ending from c@@toronto@edu  Thu May  3 17:01:52 2018
From: r@dford @ending from c@@toronto@edu (Radford Neal)
Date: Thu, 3 May 2018 11:01:52 -0400
Subject: [Rd] Proposed speedup of ifelse
In-Reply-To: <mailman.46997.7.1525341602.26470.r-devel@r-project.org>
References: <mailman.46997.7.1525341602.26470.r-devel@r-project.org>
Message-ID: <20180503150152.GA23984@mail.cs.toronto.edu>

> 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




[Rd] Proposed speedup of ifelse
Radford Neal