PDA

View Full Version : Usefulness of static allocation ?



Windsoarer
28th February 2010, 08:32
Hi,

This question is about speeding up things when performing repetitive calculations such as drawing on a widget.

Will it be faster if the local variables are allocated statically rather than on the heap ?
What about QStrings : since their size can vary, is there any point to declaring them as 'static' ?

I tried both and can't see much difference, but I can't measure it precisely either :(

Thanks for any insight in these matters

caduel
28th February 2010, 20:06
maybe static is a tiny bit faster, you won't notice that, though.

The key is not to do a faster recalculation, but to avoid it if it is not necessary (i.e. cache your result(s))
and only recalculate when and what you need to.

Did you use a profiler to detect your bottleneck? Otherwise I would not think about stuff
like static vs heap allocation.

squidge
28th February 2010, 20:16
Well, if the objects are static, they only get created once, but the overall answer to your question depends on how many times they are created otherwise (are they in a tight loop for example?)

My advice would be to not do premature optimisation. Profile your code, find the slow parts and optimise those. Ignore all other code, even if it looks slow. The time spent on such stuff normally isn't worth the payback, and you risk introducing bugs.

Windsoarer
28th February 2010, 20:45
OK thanks - I didn't know about 'profilers".

Any advice on the one to use ?

squidge
28th February 2010, 23:01
Well, that depends on your compiler, OS, and processor. Visual Studio comes with a profiler, as does GCC. If you use an AMD processor, then AMD do a very good profiler which can go into great depth. Intel do a similar profiler for Intel processors, but the AMD one was free last time I checked.

If your using GCC and don't mind command lines, have a look at 'gprof'. Not as good as the processor-specific ones, but it might be enough, depending on the level of profiling you want.

Have a look here: http://linuxgazette.net/100/vinayak.html