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.
Z
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.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.