PDA

View Full Version : Foreach performance



jano_alex_es
17th November 2009, 12:59
Hi, I have to do several long iterations in a not so powerfull device and I'm a bit worried about performance.

What's better? use a normal for or use foreach instead. For example, iterating all over a list.

Foreach seems to be the answer... but it makes a copy, so it needs extra memory.

thanks!

Lykurg
17th November 2009, 13:12
Foreach seems to be the answer... but it makes a copy, so it needs extra memory.

Yes, but since most of the Qt classes uses implicit sharing it isn't a problem. Just make sure you use const! this will save a lot of time.
And if that is still to slow for you, use const STL-style iterators, they are the fastest. And if you think that your source code is unreadable with STL iterators, make a quick benchmark on your device and see if they are really quicker.

spirit
17th November 2009, 13:26
read this (http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/) post, but it deals with Qt classes.