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

M. Bashir Al-Noimi mbnoimi at gmx.com
Sat Nov 28 23:28:03 CET 2009


>>> I'm sorry but that is wrong. From the docs of QList:
>>>
>>> "Internally, QList<T> is represented as an array of pointers to items of
>>> type T. If T is itself a pointer type or a basic type that is no larger
>>> than a pointer, or if T is one of Qt's shared classes, then QList<T>
>>> stores the items directly in the pointer array."
>>>
>>> So for types where sizeof( type ) <= sizeof( pointer ) a QList is just
>>> the same as a QVector - except that it also pre-allocates extra space at
>>> the beginnging whereas QVector doesn't.
>>>       
>> Ah, good to know. When I use it, I use it under the assumption that it
>> acts like a linked list. I prefer QVector<> for things that are meant to
>> be stored contiguously.
>>
>> IMHO, having QList<> act like a QVector<> under certain circumstances is
>> an implementation detail that muddies the waters a bit.
>>     
>
> QList always has constant access time though. It is just that when the type 
> being stored is larger than a pointer, the constant also includes the time to 
> perform the extra pointer redirection since QList stores a vector of pointers 
> in this case.
>
> This table compares the speed of various operations on the Qt container 
> classes:
>
> http://doc.trolltech.com/4.6-snapshot/containers.html#algorithmic-complexity
>   

Sean and Paul, your discussion is truly useful thanks for you ;-)

By the result I think using "for" with "QList" in classic usage is more 
safe because it gives us same performance in most cases.


-----
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/20091129/78ccbfeb/attachment.html 


More information about the Qt-interest-old mailing list