[Merkaartor] Small speedup (const variables)

Dirk Stöcker openstreetmap at dstoecker.de
Sat Jun 21 16:33:47 BST 2008


On Sat, 21 Jun 2008, Roeland Douma wrote:

> I have alway learned that const values are better. Also for maintaing it

For basic types (int,char,float,...) there is no real difference, as they 
are passed by value anyway and the function has full control (and 
the compiler thus also can decide if it is const or not). One difference 
is there - if you use the value as an variable for writting, then this may 
be less efficent compared to a local variable (thought for a good compiler 
there should be no difference).

Another topic are pointers and references. He you are right. It is always 
better to have them const, but usually not due to optimizations, but due 
to code mainainability.

Also for complex types (structures, classes) const may help. A compiler 
could e.g. decide to pass it by reference instead of copying it. But 
passing such arguments by value is anyway bad design. References and 
pointers are better here.

> yourself since the compiler tells you when someting is overwritten
> (espessialy when you don't want it). But I do not maintain the code so
> ofcoures it is up to you guyz :)
>
> I'll check the code for some pointers that can be made const and functions
> that can be marked static. As my contribution to the project :)

Search for QString arguments. Usually anything like "QString a" should 
better be "const QString &a". Also "QString &a" often should be "const 
QString &a". If you find these, you really do optimization (thought you 
compete with newer compilers, which try to find such cases themselves).

But when changing this like that, be sure you do it right :-)

Best approach to do optimization is another one:
Run the compiled source with profiling code, after doing some work check 
the profiler output. Find the most often called and most time using 
functions and code lines. See if you can optimize these. Where the 
unspecific code optimizations may bring 0.01% speed gain, here you can get 
things between 1% and 500000%.

Ciao
-- 
http://www.dstoecker.eu/ (PGP key available)




More information about the Merkaartor mailing list