[kgraphviewer-devel] Re: optimize map loops

Gaël (aka Kleag) kleag at free.fr
Sun Nov 7 18:22:15 CET 2010


Hi Millian,

Yes, I must confess that when I discovered the "foreach" of Qt, I found it so 
easy to use that I did not try to optimize :-(

Gaël
Le dimanche 7 novembre 2010 16:22:10, Milian Wolff a écrit :
> Hey Gael,
> 
> Albert reviewed parts of kgraphviewer and spotted things like this:
> 
>   QList<QString> edgesKeys = edges().keys();
>   foreach (const QString& eid, edgesKeys)
>   {
>     GraphEdge* edge = edges()[eid];
> 
> This is like the worst possible way to iterate over maps you can imagine.
> Instead you should write it like this:
> 
>   QMap<QString, GraphEdge*>::const_iterator it = edges().constBegin();
>   while (it != edges().constEnd()) {
>      // use it.value() and it.key() here
>      ++it;
>   }
> 
> Though looking at the sources, one can even rewrite them much simpler, I'll
> commit it, please review it then.
> 
> Bye

-- 
KsirK - a world domination strategy game 
http://techbase.kde.org/Projects/Games/Tactic_and_Strategy/KsirK

KGraphViewer - a GraphViz dot graphs viewer and editor based on a reusable 
part
http://extragear.kde.org/apps/kgraphviewer


More information about the kgraphviewer-devel mailing list