Skip to content
Prev 276303 / 398506 Next

Is it possible to vectorize/accelerate this?

I don't immediately see a good trick for vectorization so this seems to me to be a good candidate for work in a lower-level language. Staying within R, I'd suggest you use if and else rather than ifelse() since your computation isn't vectorized: this will eliminate a small amount over overhead. Since you also always add a_vec, you could also define b_vec as a copy of a to avoid all those calls to subset a, but I don't think the effects will be large and the code might not be as clear. 

You indicated that you may be comfortable with writing C, but I'd suggest you look into the Rcpp/Inline package pair which make the whole process much easier than it would otherwise be.

 I'm not at a computer write now or I'd write a fuller example, but the documentation for those packages is uncommonly good an you should be able to easily get it down into C++. If you aren't able to get it by tomorrow, let me know and I can help troubleshoot. The only things I foresee that you'll need to change are zero-basing, C's loop syntax, and (I think) the call to abs(). (I always forget where abs() lives in c++ ....)

The only possible hold up is that you need to be at a computer with a C compiler

Hope this helps,

Michael
On Nov 3, 2011, at 3:10 PM, hihi <v.p.mail at freemail.hu> wrote: