Performance Tip Optimizing Iteration

From Qt Wiki
Revision as of 16:16, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

Java style iterator

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.

Additional reading

Some of this material originally appeared in Maximizing Performance with Qt [qt.nokia.com] white paper.

Categories: