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

M. Bashir Al-Noimi mbnoimi at gmx.com
Sat Nov 28 15:30:04 CET 2009


David Ching wrote:

> "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"
Thanks David, this article gave the right answer what I'm looking for, 
but I still have another question.

*Can I use "foreach" for modifying container items just like for?*




-----
Best Regards
Muhammad Bashir Al-Noimi
My Blog: http://mbnoimi.net/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091128/6e837a25/attachment.html 


More information about the Qt-interest-old mailing list