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.

Performance Tip Optimizing Iteration: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(Convert ExpressionEngine section headers)
Line 30: Line 30:
When using STL-style iterators with a list containing complex items, execution is faster if you use the +''i operator instead of the i''+ operator. The i++ will force your loop to work on a copy of the item i.  
When using STL-style iterators with a list containing complex items, execution is faster if you use the +''i operator instead of the i''+ operator. The i++ will force your loop to work on a copy of the item i.  


h2. Careful with foreach
== Careful with foreach ==
 


Benchmarking Qt applications indicates there is always a performance penalty to using a foreach loop as opposed to a for loop with an iterator. However, you can greatly reduce the performance penalty if you use a const iterator in your foreach loop. This can often make the performance penalty negligible, though it is never zero.
Benchmarking Qt applications indicates there is always a performance penalty to using a foreach loop as opposed to a for loop with an iterator. However, you can greatly reduce the performance penalty if you use a const iterator in your foreach loop. This can often make the performance penalty negligible, though it is never zero.
Line 40: Line 39:
</code>
</code>


h2. Additional reading
== Additional reading ==
 
''' [http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ Iterating efficiently] - Qt Labs blog post
''' [http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ Iterating efficiently] - Qt Labs blog post

Revision as of 16:07, 5 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

[toc align_right="yes" depth="2"]

Performance Tip: Optimizing Iteration

Here are two tips to help produce code that makes the most out of Qt internal performance.

Iterator selection

Qt allows you to use both Java-style and STL-style iterators to step through your structures. Your selection of iterator style has no performance impact, but your selection of iterator operator does.

QListIterator<int> i(list);
while (i.hasNext())
 process(i.next());

Java style iterator

QList<int>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
 process('''i);

STL (standard template library) style iterator

When using STL-style iterators with a list containing complex items, execution is faster if you use the +i operator instead of the i+ operator. The i++ will force your loop to work on a copy of the item i.

Careful with foreach

Benchmarking Qt applications indicates there is always a performance penalty to using a foreach loop as opposed to a for loop with an iterator. However, you can greatly reduce the performance penalty if you use a const iterator in your foreach loop. This can often make the performance penalty negligible, though it is never zero.


foreach (const QString &amp;i, list)
 process(i);

Additional reading

Iterating efficiently - Qt Labs blog post