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

Ken-JP kfmfe04 at gmail.com
Mon Mar 23 09:49:41 CET 2009

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.

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"

> dim( m )
[1] 4518  800
> system.time( x.rollapply <- pchg.rollapply( m, 20 ) )
   user  system elapsed 
 146.94    0.81  157.03 
> system.time( x.inline    <- pchg.inline( m, 20 ) )
   user  system elapsed 
   0.69    0.00    0.72 

View this message in context: http://www.nabble.com/performance%3A--zoo%27s-rollapply%28%29-vs-inline-tp22656214p22656214.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list