ArlexBee-871RBO
27th December 2009, 20:58
Greetings.
I'm very new to QtConcurrent, and I have very little experience in threading in general. I went over some examples today in the Qt docs, and I can't figure out why my multi-threaded code runs slower than the single-threaded version.
This code is basically a modified version of what I found in the docs.
#include <QThread>
#include <QApplication>
#include <qtconcurrentmap.h>
#include <iostream>
#include <QtConcurrentRun>
using namespace std;
int Func(const int &n){
//Some random calculations...
int a = rand() % 3, b = rand() % 4, c = rand() % 5;
int x = a * a, y = b * b, z = c * c;
int u = a + x, v = b + y, w = c + z;
int d = u - v * w;
if( d > 0 ){
d = a / x / u;
}else{
d = b / y / v;
}
for(int i = 1; i < 10; ++i){
for(int j = 1; j < 30; ++j){
d = rand() % 5 + 3;
d /= 2;
d *= rand() % 4;
}
}
return d * n;
}
int main(int argc, char *argv[]){
QApplication app(argc, argv);
const int SIZE = 500000;
QVector<int> data;
for(int i = 0; i < SIZE; ++i){
data.push_back(i);
}
//non-concurrent version
//for(int i = 0; i < SIZE; ++i){ data[i] = Func( data[i] ); }
//concurrent version
QtConcurrent::mapped(data, Func);
return 0;
}
I'm running it on a dual core, and the single-threaded version runs in about 0.7s, but the using QtConcurrent it takes over 30.0s. The two cores are utilized, but not at 100%.
Also, using QtConcurrent::map() I get seg-fault, and I'm not sure why.
I'm probably doing something wrong, but can anyone tell me why QtConcurrent::mapped() call slows it down so much??
I'm very new to QtConcurrent, and I have very little experience in threading in general. I went over some examples today in the Qt docs, and I can't figure out why my multi-threaded code runs slower than the single-threaded version.
This code is basically a modified version of what I found in the docs.
#include <QThread>
#include <QApplication>
#include <qtconcurrentmap.h>
#include <iostream>
#include <QtConcurrentRun>
using namespace std;
int Func(const int &n){
//Some random calculations...
int a = rand() % 3, b = rand() % 4, c = rand() % 5;
int x = a * a, y = b * b, z = c * c;
int u = a + x, v = b + y, w = c + z;
int d = u - v * w;
if( d > 0 ){
d = a / x / u;
}else{
d = b / y / v;
}
for(int i = 1; i < 10; ++i){
for(int j = 1; j < 30; ++j){
d = rand() % 5 + 3;
d /= 2;
d *= rand() % 4;
}
}
return d * n;
}
int main(int argc, char *argv[]){
QApplication app(argc, argv);
const int SIZE = 500000;
QVector<int> data;
for(int i = 0; i < SIZE; ++i){
data.push_back(i);
}
//non-concurrent version
//for(int i = 0; i < SIZE; ++i){ data[i] = Func( data[i] ); }
//concurrent version
QtConcurrent::mapped(data, Func);
return 0;
}
I'm running it on a dual core, and the single-threaded version runs in about 0.7s, but the using QtConcurrent it takes over 30.0s. The two cores are utilized, but not at 100%.
Also, using QtConcurrent::map() I get seg-fault, and I'm not sure why.
I'm probably doing something wrong, but can anyone tell me why QtConcurrent::mapped() call slows it down so much??