Performance Tip Optimizing Iteration: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Performance Tip: Optimizing Iteration=
[[Category:Learning]]<br />[[Category:HowTo]]<br />[[Category:Developing with Qt]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
 
= 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.


==Iterator selection==
== 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.


Qt allows you to use both Java-style and <span class="caps">STL</span>-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&amp;lt;int&amp;gt; i(list);<br />while (i.hasNext())<br /> process(i.next());<br /></code>


''Java style iterator''
''Java style iterator''


''<span class="caps">STL</span> (standard template library) style iterator''
<code>QList&amp;lt;int&amp;gt;::iterator i;<br />for (i = list.begin(); i != list.end(); ++i)<br /> process('''i);<br /></code>
 
<br />''STL (standard template library) style iterator''
When using <span class="caps">STL</span>-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.
<br />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.  
 
<br />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.
 
==Additional reading==
 
* [http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ Iterating efficiently] ''[labs.trolltech.com]'' – Qt Labs blog post
 
''Some of this material originally appeared in [http://qt.nokia.com/products/library/forms/whitepapers/reg-whitepaper-performance Maximizing Performance with Qt] ''[qt.nokia.com]'' white paper.''


===Categories:===
<br />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.


* [[:Category:Developing with Qt|Developing_with_Qt]]
<br /><code>foreach (const QString &amp;i, list)<br /> process(i);<br /></code>
* [[:Category:HowTo|HowTo]]
<br />h2. Additional reading
* [[:Category:Learning|Learning]]
<br />''' &quot;Iterating efficiently&amp;quot;:http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ - Qt Labs blog post

Revision as of 10:19, 24 February 2015




[toc align_right="yes&quot; depth="2&quot;]

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&amp;lt;int&amp;gt; i(list);<br />while (i.hasNext())<br /> process(i.next());<br />

Java style iterator

QList&amp;lt;int&amp;gt;::iterator i;<br />for (i = list.begin(); i != list.end(); ++i)<br /> process('''i);<br />


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.
h2. 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)<br /> process(i);<br />


h2. Additional reading
"Iterating efficiently&quot;:http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/ - Qt Labs blog post