QtConcurrent-simple-usage: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
Hello,
Hello,


This code focuses on the following problem:<br />We have a '''QList&amp;lt;Params&amp;gt;'''<br />We need to run some time consuming function for each '''Params''' set on the list.
This code focuses on the following problem:
We have a '''QList<Params>'''
We need to run some time consuming function for each '''Params''' set on the list.


The single core solution:<br /><code><br />void Optimizer::execute()<br />{<br /> QList&amp;lt;Params&amp;gt; params = generateParamsSet();<br /> QList&amp;lt;Result&amp;gt; results;<br /> for( int i = 0; i &lt; params.size(); +''i ) {<br /> results.append( someReallyTimeConsumingFunction(params) );<br /> }<br /><br /></code>
The single core solution:
<br />Let's use QtConcurrent to scale easily over the cores!
<code>
<br /><code><br />void Optimizer::execute()<br />{<br /> QList&amp;lt;Params&amp;gt; params = generateParamsSet();<br /> QList&amp;lt;Result&amp;gt; results = QtConcurrent::blockingMapped( params, &amp;Optimizer::calculateStats );<br /> <br />}
void Optimizer::execute()
<br />Result Optimizer::calculateStats( const Params &amp;params )<br />{<br /> return someReallyTimeConsumingFunction( params ); //this function returns the Result object.<br />}<br /></code>
{
<br />Using it that way, we are sure that none of the cores is bored during our processing!
QList<Params> params = generateParamsSet();
<br />To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this:<br /><code><br />void Optimizer::execute()<br />{<br /> QList&amp;lt;Params&amp;gt; params = generateParamsSet();<br /> QList&amp;lt;Result&amp;gt; results;<br /> #ifndef QT_NO_CONCURRENT<br /> results = QtConcurrent::blockingMapped( params, &amp;Optimizer::calculateStats );<br /> #else<br /> for( int i = 0; i &lt; params.size();''+i ) {<br /> results.append( someReallyTimeConsumingFunction(params) );<br /> }<br /> #endif<br /><br />}
QList<Result> results;
for( int i = 0; i < params.size(); +''i ) {
results.append( someReallyTimeConsumingFunction(params) );
}
</code>
 
Let's use QtConcurrent to scale easily over the cores!
 
<code>
void Optimizer::execute()
{
QList<Params> params = generateParamsSet();
QList<Result> results = QtConcurrent::blockingMapped( params, &amp;Optimizer::calculateStats );
}
 
Result Optimizer::calculateStats( const Params &amp;params )
{
return someReallyTimeConsumingFunction( params ); //this function returns the Result object.
}
</code>
 
Using it that way, we are sure that none of the cores is bored during our processing!
 
To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this:
<code>
void Optimizer::execute()
{
QList<Params> params = generateParamsSet();
QList<Result> results;
#ifndef QT_NO_CONCURRENT
results = QtConcurrent::blockingMapped( params, &amp;Optimizer::calculateStats );
#else
for( int i = 0; i < params.size();''+i ) {
results.append( someReallyTimeConsumingFunction(params) );
}
#endif
}

Revision as of 11:15, 25 February 2015


Hello,

This code focuses on the following problem: We have a QList<Params> We need to run some time consuming function for each Params set on the list.

The single core solution:

void Optimizer::execute()
{
 QList<Params> params = generateParamsSet();
 QList<Result> results;
 for( int i = 0; i < params.size(); +''i ) {
 results.append( someReallyTimeConsumingFunction(params) );
 }

Let's use QtConcurrent to scale easily over the cores!

void Optimizer::execute()
{
 QList<Params> params = generateParamsSet();
 QList<Result> results = QtConcurrent::blockingMapped( params, &amp;Optimizer::calculateStats );
 
}

Result Optimizer::calculateStats( const Params &amp;params )
{
 return someReallyTimeConsumingFunction( params ); //this function returns the Result object.
}

Using it that way, we are sure that none of the cores is bored during our processing!

To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this: void Optimizer::execute() {

QList<Params> params = generateParamsSet();
QList<Result> results;
#ifndef QT_NO_CONCURRENT
results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats );
#else
for( int i = 0; i < params.size();+i ) {
results.append( someReallyTimeConsumingFunction(params) );
}
#endif

… }