Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to optimize Containers: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
m (repaired broken link)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:Learning]]
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Developing with Qt]]
{{WarningBox|text=This article contains missing references. Feel free to add them and remove this warning afterwards}}
[toc align_right="yes" depth="2"]
The developers of the Qt framework spend a lot of time packing performance into their code. The Qt 4 release back in 2008 focused on performance, and they released some documentation at the time to help developers leverage the upgrade. Some of the discussions at the time are still worth reviewing, including a discussion about container optimization summarized here. A detailed discussion of performance issues is available in the Maximizing Performance with Qt white paper <span style="color:rgb(200,100,100);font-size:.8em"><nowiki>[Reference missing]</nowiki></span>.
 
= Performance Tip: Optimizing Containers =
 
The developers of the Qt framework spend a lot of time packing performance into their code. The Qt 4 release back in 2008 focused on performance, and they released some documentation at the time to help developers leverage the upgrade. Some of the discussions at the time are still worth reviewing, including a discussion about container optimization summarized here. A detailed discussion of performance issues is available in the "Maximizing Performance with Qt":http://qt.nokia.com/products/library/forms/whitepapers/reg-whitepaper-performance white paper.


== Container selection ==
== Container selection ==
Line 16: Line 9:
Let's continue with three representative container classes and how container type impacts performance.
Let's continue with three representative container classes and how container type impacts performance.


* QVector is optimized for accessing data at the expense of slowing the process of data insertion and deletion. The QVector class stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory.
* {{DocLink|QVector}} is optimized for accessing data at the expense of slowing the process of data insertion and deletion. The QVector class stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory.


* QLinkedList is optimized for the other extreme: this classic linked list pattern makes insertion and deletion of items lightning quick, but accessing an item requires iteration over the list.
* {{DocLink|QLinkedList}} is optimized for the other extreme: this classic linked list pattern makes insertion and deletion of items lightning quick, but accessing an item requires iteration over the list.


* QList is by far the most commonly used container class. More than any other container class, QList is highly optimized to expand to as little code as possible in the executable. It stores a list of values of a given type that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.  
* {{DocLink|QList}} is by far the most commonly used container class. More than any other container class, QList is highly optimized to expand to as little code as possible in the executable. It stores a list of values of a given type that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.  


== Additional reading ==
== Additional reading ==


* "Maximizing Performance with Qt":http://qt.nokia.com/products/library/forms/whitepapers/reg-whitepaper-performance white paper.  
* [http://doc.qt.io/qt-5/containers.html Container class documentation from 5.x release]
* "Container class documentation from 4.6 release":http://doc.trolltech.com/4.6/containers.html
* [http://doc.qt.io/archives/qq/qq19-containers.html Inside the Qt 4 Containers] - Qt Quarterly #19 article about internal implementation
* "Inside the Qt 4 Containers":http://doc.qt.nokia.com/qq/qq19-containers.html - Qt Quarterly #19 article about internal implementation

Latest revision as of 18:50, 29 May 2017

This article contains missing references. Feel free to add them and remove this warning afterwards

The developers of the Qt framework spend a lot of time packing performance into their code. The Qt 4 release back in 2008 focused on performance, and they released some documentation at the time to help developers leverage the upgrade. Some of the discussions at the time are still worth reviewing, including a discussion about container optimization summarized here. A detailed discussion of performance issues is available in the Maximizing Performance with Qt white paper [Reference missing].

Container selection

Let's start with the obvious: containers are fundamental building blocks of software applications. How quickly data can be moved into and out of containers is a critical factor in the application's overall performance. Qt 4.5 included optimizations which sped up the performance of most containers, which means better performance for all Qt applications. The optimization of containers has continued through the 4.6 and upcoming 4.7 releases.

Let's continue with three representative container classes and how container type impacts performance.

  • QVector is optimized for accessing data at the expense of slowing the process of data insertion and deletion. The QVector class stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory.
  • QLinkedList is optimized for the other extreme: this classic linked list pattern makes insertion and deletion of items lightning quick, but accessing an item requires iteration over the list.
  • QList is by far the most commonly used container class. More than any other container class, QList is highly optimized to expand to as little code as possible in the executable. It stores a list of values of a given type that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.

Additional reading