[R] performance: zoo's rollapply() vs inline

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 23 10:34:13 CET 2009


On Mon, Mar 23, 2009 at 5:08 AM, Achim Zeileis
<Achim.Zeileis at wu-wien.ac.at> wrote:
> On Mon, 23 Mar 2009, Ken-JP wrote:
>
>> zoo's rollapply() function appears to be extremely useful for plugging in
>> a
>> function on-the-fly to run over a window.  With inline, there is a lot
>> more
>> coding and room for error, and the code is less portable because the user
>> has to have R compiling set up or it won't work.
>>
>> However, rollapply() seems to be really slow.  Several orders of magnitude
>> slower than inline, in fact.  I don't know how to call R functions from C
>> inline yet, but it looks like I need to learn, because the speed
>> difference
>> is just way too big.
>
> It depends what you want to do with it. If you use rollapply() for
> operations that you could do in a vectorized way then it is certainly not a
> good idea (see below). Important functions, especially rolling means, are
> special cased and are much faster than a regular rollapply().
>
>> The results of a quick test are shown below.
>>
>> I am totally open to suggestions on how to do windowed calculations, in
>> general, but it looks like I may have to bite the bullet and learn all the
>> intricacies of calling R from C.
>>
>> NOTE:  pchg.inline() is not shown because it's much longer/complex than
>> pchg.rollapply(), but I am doing no optimizations.
>>
>>
>> ------------------------------------------------------------------------------------------------------------
>>
>> pchg.rollapply <-   function(this, m, shift=1, ...)  {
>>  rollapply( m, shift+1, function(x) { x[shift+1]/x[1] - 1; },
>> align="right"
>> );
>> }
>
> This is really a bad example because your function is flawed (no dependence
> on "this") and it is not clear to me why you would want to use rollapply().
> Just doing
>  m/lag(m, -shift) - 1
> should do the job.

Also check out ?diff.zoo and note the arithmetic = FALSE argument although
using lag.zoo as above may be slightly faster.

If the above is not your real problem also check out rollmean, rollmedian
and rollmax which are also written in R but have been optimized for those
operations.

The xts package has some routines written in C that may also be applicable.




More information about the R-help mailing list