My final year project is to create a digital image stabilisation program in C++ using openCV. The idea is to stabilise a live feed from a HD webcam. In an ideal world it should be running at 25FPS, but the best I can do is 15-20FPS in release mode. Does anyone have any tips for optimising the code using any of the options in visual studios 2012? I'm not fussed by compile time, program size or memory footprint. Its a pretty small program which, at worst, will have a few pictures in its memory so all I'm looking for is speed. I've looked at a couple of the obvious settings in the optimisations section of the options and they haven't really made a noticeable difference.
Tip 1, never optimize unless you have to.
Tip 2, never optimize without doing detailed profiling. I use a Valgrind add on that does profiling of calls combined with taking manual measurements.
Tip 3, it is always more beneficial to choose the right algorithm and data structure rather than do micro optimizations, e.g. you might be doing something that runs in O(n^2) when a different algorithm runs in O(nLogn), or you might be iterating over a list to find a particular element when a map would be faster. Pre-allocation and pre-computation can be very useful as well (e.g., if you know ahead of time a vector will hold 100 elements than reserve space for 100 upfront and avoid dynamic resizing).
For vision processing many things are much fast when done within frequency domain, so do a FFT and and apply all convolutions and such like in frequency domain and convert back m(and see if you process images as powers of 2).
Tip 3b) Make Sure you a fully using you CPU multi thread your code where it makes sense use the vector support where you can, e.g. use proper Linear algebra libraries etc. Vision processing is invariably perfect for multi-thread, with video you can send alternate frames to separate threads, or a single frame can be split and processed independently.
Tip 4: mico optimization is hard because it depends on the compiler and architecture, something are expensive some cheap, it is hard to know which without detailed testing.
Tip 4B) make sure you don't have any bugs, it can be very easy to pass large object by value rather than reference (accidentally delete an ampersand &)
Tip 5: typically optimization can lead to ugly code that is harder to maintain.
E.g., every function call has overhead which will add up in a loop, inline functions are only recommendations to the compiler so to guarantee inline performance you need to forget the function and add the code within the loop. This can get ugly fast and make testing hard, but sometimes it is the only way.
You would be surprised what things are fast and slow.