There are four basic methods of speeding up a chunk of Octave code (in order of increasing speed):
- Allocate memory smartly (using for loops)
- Vectorize your code (avoiding for loops)
- Use sparse matrix operators in place of for loops you can't vectorize
- Write a compiled function in C/C++/Fortran to call from Octave
The first three options keep you in pure Octave. The fourth option, while requiring you to use a compiled language is really not too bad because you can use all of the types (vectors, matrices, etc) defined in the Octave libraries to write your function. That's almost as easy as doing it in Octave.
Speeding Up For Loops
This one really amounts to not being foolish. The only way to speed up for loops is to make sure you allocate all of your memory (using
zeros()for instance) before you enter the for loop. The Octave Manual covers this topic here. The better thing to do is vectorize your code and avoid for loops all together.
What you really want to do is cast your calculation as a series of vector and matrix operations, or use the built-in (usually compiled) functions that operate directly on vectors. The post Is Octave Slow? demonstrates the sorts of speed up you can get when you get rid of for loops.
Sparse Matrices for Data Dependancy
What about for loops you can't vectorize? The ones where the result at iteration n depends on what you calculated in iteration n-1, n-2, etc. You can define a sparse matrix that represents your for loop and then do a sparse matrix solve. This is much faster because you are now using compiled functions. The drawback is that you are trading some increase (sometimes substantial) in memory usage to buy this speed up.
- Is Octave Slow? covers this approach as well.
- More on Speeding up Octave covers the details on sparse matrix allocation.
The manual has a good intro on using
mkoctfileto compile your function and make it callable from Octave. Probably the most useful thing is that you can use the Octave types, so you don't have to code up your own matrix or vector type. At this point the speed really has little to do with Octave and everything to do with the quality of the compiler you use and how efficiently you code up your calculation.
The real power of Octave isn't that you can get your calculations to be nearly as fast as if you wrote them all in a compiled language. It's that you can prototype a calculation quickly and make sure that it is correct, and then there is a fairly well defined path to follow to speed up your correct calculation.