some test apps

Juergen Pfennig info at j-pfennig.de
Thu Oct 28 22:59:42 CEST 2004


On Wednesday 27 October 2004 05:36, Benjamin Meyer wrote:

>  const QStringList::const_iterator cend = list.constEnd();
>         for ( QStringList::const_iterator it = list.begin(); it != cend;
> ++it )
>  {
>         }

Dear Benjamin,
a while ago somebody posted that ++it would give a 5% performance improvement 
over it++ (using a templated QT Dictionary object for QCString). I tried to 
verify that and found something even better: I got a 20% improvement! My 
benchmark was like the one that you quoted (empty body of the loop).

But a short evaluation showed that the difference in execution time was only 
2ns per iteration, or in other words only 1 to 3 machine instructions per 
iteration were saved. This could even change (sign) if the body where not 
empty - I did not yet try that. Remark: Qt was not designed by beginners, 
they have avoided many stupid design problems (like creating objects in an 
iterator).

Please be very careful with such optimizations, they will have almost zero 
effect on real performance (where the loop body is not empty). And on the 
other side the code could get accidentially corrupted. In the case above the 
caching of cend makes the code less readable. Usually the list.end() or 
similar things are inline, and usually the compiler will optimize that stuff 
away.

Another point: the code that you quoted above changes the logic! If the body 
of your loop calls a function that modifies the list (here: removes the last 
element) he code could crash!!!! DANGER !!!!

I do not want to discourage you, but usually you get the best optimization 
effect by rewriting some code. Doing so makes the code often shorter, better 
to read and easier to maintain. It also helps to fix bugs.

Yours Jürgen


More information about the Kde-optimize mailing list