Any tips for optimising code in Visual studios 2012?

Soldato
Joined
15 Feb 2011
Posts
10,234
Location
Slough
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.
 
Associate
Joined
10 Nov 2013
Posts
1,808
It'll be next to impossible for anyone to optimise the code without seeing the code.

This.

It's impossible to give any useful advice without knowing how the code is written. As a general rule I would be looking at how I could write the code in a more efficient manner rather than relying on visual studio magic.
 
Man of Honour
Joined
13 Oct 2006
Posts
92,063
Compiler optimisations for the most part won't give meaningful gains for that kind of task. You'd need to profile your code to see which parts the most time was spent in and see what you can optimise there - if your using 3rd party libs for image processing then possibly not a lot you can do in that regard. If you have hand coded image processing routines then you'd have to look at more efficent ways of doing slow functions like pixel read/writes, etc. i.e. block blitting when you don't need to handle individual pixels and so on.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
"optimising" isn't a case of switching a few toggles here and there. There isn't an "optimise" magic option. They mean refactor and change your code to be more optimal; to avoid expensive (time and/or computer resource) operations where possible. Such things could be to choose a specific sorting/finding algorithm over the others based on your needs, or if you are regularly connecting to a remote service to use a local cache etc.

It's also hard to be generic/abstract when it comes to discussing optimisation as each optimisation is typically as unique as the thing you are trying to optimise.
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
32,623
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.
 
Soldato
Joined
13 Feb 2003
Posts
6,157
DP mentioned valgrind - that's not an option if you're using visual studio.

You have two practical choices:
Get someone who knows how to code C++ to review your code.
Get a profiler.

Preferably do both.
 
Associate
Joined
20 Aug 2010
Posts
1,099
Location
Not Coventry
Don't just rely on profiling tools to tell you things, learning to step back and objectively review your own code is an important industry skill. Try taking some time away from the project then coming back and asking yourself if what you have done:
- matches the requirements
- matches the design
- is correct (S/W bugs, incorrect design)
- can be optimised (different algorithms, data types, etc.)
- can be optimised within the time frame
 
Soldato
OP
Joined
15 Feb 2011
Posts
10,234
Location
Slough
Ok, so I guess there's not a lot that visual studios can do, thats a shame but pretty much what I suspected. Thanks for the advice though

Rroff, as you suspected the main culprits for hogging the performance are one or two functions in openCV that are critical to the program and that I just don't have time to hand code. I'm not going to pretend that my code is anything resembling well written, but its just one or two openCV functions that are taking a seriously long time to compute. (goodFeaturesToTrack() is the biggest problem if you are interested)
 
Soldato
Joined
13 Feb 2003
Posts
6,157
well, you're not going to be optimising library methods. What you can look at is how many times you call the slow methods and analyse whether you really need all the calls. Some operations are just cpu intensive...
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Most of what needs to be said has been said, so I don't really have anything more to add other than say that the program you're using is called Visual Studio, singular.
WTF is Visual Studios?
 
Back
Top Bottom