[Qt-interest] What is the best, "for" or "foreach"?

David Ching dc at remove-this.dcsoft.com
Sat Nov 28 05:12:59 CET 2009


"M. Bashir Al-Noimi" <mbnoimi at gmx.com> wrote in message 
news:4B1068F5.1010201 at gmx.com...
> Hi All,
>
> I've wrote the following snippet for testign the speed of "for" and 
> "forech" loops
> and I noticed that "foreach" faster than "for" about 2 times! >:o
>
> Is "foreach" faster than "for"?
>

See http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/

"Bottom line: If you - like me - value the readability of foreach, you 
should probably get used to writing foreach (const Type &, list) (that is, 
whenever you do not alter the value). Otherwise the different styles of 
iterating do not really differ performance wise. Whatever you do in the loop 
body will in most cases have a much larger impact"

-- David




More information about the Qt-interest-old mailing list