How to speed up Octave Loops by 20-30x with C++

This is a companion discussion topic for the original entry at
1 Like

This video shows how to write a simple Oct-file that allows speeding-up of Octave loops by a large factor. While it is usually best to stick with Octave and write high-level code, and use vectorization and follow Octave best practices, there are instances in which a portion of the code needs to be written in a lower level language. Using Oct-files does not always translate to better speeds, but for several nested for-loops, as in the example (taken from Hartree-Fock or Kohn-Sham theories), it accelerates the execution by a factor of 20 or 30. This code can probably be improved, both in Octave and C++, and if anyone has suggestions I would greatly appreciate them. Particularly I don’t know of a way of using matrix operations to avoid this 4-D loop.
If anyone finds the videos useful I would appreciate pressing the like button, and suscribing to the channel. Also leaving questions and suggestions in the video comments!
Thank you very much!