Monday, January 5, 2009

How to Speed Up Octave

Speed seems like a continuing theme on the Octave mailing lists, a recent thread on the list of "comparing the executive speed with Matlab" demonstrates that there really is very little speed overhead when you use Octave smartly. If you read down far enough, it also demonstrates that the Intel Fortran compiler is awesome!

There are four basic methods of speeding up a chunk of Octave code (in order of increasing speed):
  1. Allocate memory smartly (using for loops)

  2. Vectorize your code (avoiding for loops)

  3. Use sparse matrix operators in place of for loops you can't vectorize

  4. 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.

Vectorizing


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.


Compile It


The manual has a good intro on using mkoctfile to 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.

Conclusion


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.

5 comments:

  1. I got this request for help on speeding up a moving average calculation by email recently.

    > The original problem is
    > for j=2:length(data)
    > a(j) = a(j-1) + alpha*(data(j)-a(j-1));
    > endfor
    > Now after creating A as a sparse matrix, given the original above,
    > what is "b" in your equation "x = A\b"?
    >
    The way I do it is to think of each iteration of the loop as an equation (see this post).

    Writing out your loop I get a (alpha - 1) on the sub-diagonal, and a 1 on the main diagonal. I get an alpha*d(j) on the right-hand side, so that's your b.

    ReplyDelete
  2. You may as well have a healthy snack with you. Skipping foods nearly always leads to about-feeding on afterwards. Healthy snacking is the answer.

    Watch for more tips: https://www.youtube.com/watch?v=85eDvx_egLM

    Attempt to relax and have a breath If you have a food. You may be much more likely to sign up that you've got eaten. Bear in mind, it may take up to twenty minutes so that you can truly feel complete immediately after taking in.

    Down below are a few suggested answers to help you students deal with these interpersonal, intrapersonal, and environmental boundaries:

    ReplyDelete
  3. By way of example, If you cannot locate the the perfect time to have a brisk wander for 30 minutes each day, break up up your exercise routine into more compact segments. You usually takes a ten-moment stroll each morning, stroll ten minutes at lunch, after which do a ten-minute stroll soon after evening meal. There are not any procedures, so do what is effective for you.

    Watch for more tips: https://www.youtube.com/watch?v=QvJD82UhLt8

    In advance of starting any Health and fitness schedule, it is vital to heat up, then do some light stretching. Help you save the bulk of the stretching for following the training.

    Exercise films and DVDs. Just before Doing the job out using a household exercise video or DVD, Siegrist suggests looking at via it no less than as soon as to look at the composition and proper form of the exercise routine.

    ReplyDelete
  4. Do anything you enjoy. If the considered exercise would make you wince, Why don't you do anything you really love? If you have a enthusiasm for dancing or would like to find out how to ideal your free throws, then get in existence and get it done.

    https://www.youtube.com/watch?v=QvJD82UhLt8

    The juice on its own would not incorporate the pores and skin, seeds or fleshy center. Even so, The complete apple gives the many goodness of all the fruit and consequently provides higher overall health Homes. This instance relates to most all foods, so choosing "full foods” more than processed foods means you will get the richest offer of nutrients doable.

    Exercise ball. While Directions and/or maybe a companion video can accompany this gadget, Bracko anxieties that beginners might use exercise balls improperly. "A lot of people fall off or are unable to maintain the ball even now," he suggests. But in the event you delight in Operating out by having an exercise ball, it can offer an excellent exercise session.

    ReplyDelete
  5. You will find there's Get in touch with button beneath the gig description For each merchandise. Generally Call the seller before you shell out The cash to verify They're an excellent in shape on your job.

    Click to more: latest fiverr tips 2017

    After you start to Get the content development going on it may be a extremely excellent strategy to acquire anyone that can assist you edit it and compile it in to useful posts. new fiverr tips 2017

    ReplyDelete