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
(Remove non-functioning "toc" command)
(Cleanup)
 
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:Learning]]
[[Category:Learning]]
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Developing with Qt]]
[[Category:Developing with Qt]]
= Performance Tip: Optimizing Iteration =
Here are two tips to help produce code that makes the most out of Qt internal performance.
Here are two tips to help produce code that makes the most out of Qt internal performance.


Line 13: Line 9:
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.
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.


<code>QListIterator<int> i(list);
Java style iterator:
<code>
QListIterator<int> i(list);
while (i.hasNext())
while (i.hasNext())
process(i.next());
    process(i.next());
</code>
</code>


''Java style iterator''
STL (Standard Template Library) style iterator:
 
<code>
<code>QList<int>::iterator i;
QList<int>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
for (i = list.begin(); i != list.end(); ++i)
process('''i);
    process(*i);
</code>
</code>


''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.  
 
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 ==
== Careful with foreach ==
Line 33: Line 29:
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.


 
<code>
<code>foreach (const QString &i, list)
foreach (const QString &i, list)
process(i);
    process(i);
</code>
</code>


== Additional reading ==
== Additional reading ==
''' [http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ Iterating efficiently] - Qt Labs blog post
# Kai Köhne's blog, [https://blog.qt.io/blog/2009/01/23/iterating-efficiently/ Iterating efficiently]

Latest revision as of 21:18, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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:

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

STL (Standard Template Library) style iterator:

QList<int>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    process(*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.

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 &i, list)
    process(i);

Additional reading

  1. Kai Köhne's blog, Iterating efficiently