[SOLVED] operator [] or QList::at()?
-
Greetings.
I wonder if using the operator [] on a constant QList (or QVector) causes a deep copy.
I have a function that receives as a parameter a const QList< cv::Mat >& and l want to know if it is better access each element with the operator [] or the member function QList::at(). As far as I read this last function can be faster than operator [], but for a constant container I'm not sure if the performance is the same in both cases.
On matters of style I prefer to use the operator [], but I'm not sure which is the better option in this situation?
Thanks in advance for any help and/or suggestion.
-
Check out the documentation: the constant variant is the same as QList::at(). "Link":http://qt-project.org/doc/qt-5/qlist.html#operator-5b-5d-2.
-
Hi,
When accessing QList members it's good to know that most Qt classes use 'copy at write' operations with a reference counter. This said the performance between [] or at() might be slim for const use (no writable). So Qt will give a reference to the QList member and not a deep copy! -
Thank you all.
So in summary for this case, both options (operator [] and QList::at()) have the same or similar performance... I am right?
-
Yes, you are, as long as we are talking about const containers. In non-const onces, operator[] can insert a new default-constructed value when an item at given index is not present, thus copying the data.