Using ISO C++0x threads degrades performance
I'm writing a program in C++ using threads to improve the efficiency.
Basically I'm just creating a huge vector of integers (1 GB) and filling it with random numbers. I executed the program without threads and calculated the time needed.
Now I want to use 2 threads and see the time improvement, but the program is taking much more time with 2 threads than without. Don't know what I'm doing wrong :S
#includes...
using namespace std;
//This function just make a for from first to final. Each iteration write a random
//number in the position i of the vector
void generateRandomVector(vector<int> &vec,int first, int final);
//Inside this function i take timestamp2 and calculate the executing time
void calculateTime(clock_t start);
int main(int argc, char *argv[]){
clock_t start;
double logaritmo;
int n = 256*1024*1024;
//Taking timestamp 1
start = clock();
vector<int> vec(n);
thread t1(generateRandomVector, ref(vec), 0, n/2);
thread t2(generateRandomVector, ref(vec), n/2, n);
t1.join();
t2.join();
calculateTime(start);
I'm passing by reference the vector to both threads because I'm giving them different ranges so they will never be accessing the same position.
If needed, I can also post the generateRandomVector
function.
Hope someone can help :D
EDIT - generateRandomVector
function:
void generarRandomVector(vector<in开发者_如何学Got> &vec,int first, int final){
srand((unsigned)time(0));
//PID of each thread
cout << "PID: " << this_thread::get_id() << "\n";
for(int i = first; i < final; i++){
vec[i] = static_cast<int> ((double)rand()*(double)(vec.size()) / ((double)RAND_MAX+1.0));
vec[i] = vec[i] % 10;
}
}
Here's the complete C++0x solution, using <random>
and <chrono>
:
#include <random>
#include <chrono>
#include <thread>
#include <iostream>
#include <vector>
#include <functional>
void generarRandomVector(std::vector<int> &vec, int first, int final)
{
std::mt19937_64 e(time(0));
std::uniform_int_distribution<> d(0, 9);
//PID of each thread
std::cout << "PID: " << std::this_thread::get_id() << "\n";
for(int i = first; i < final; i++)
vec[i] = d(e);
}
int main()
{
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<double> sec;
int n = 256*1024*1024;
Clock::time_point start = Clock::now();
std::vector<int> vec(n);
std::thread t1(generarRandomVector, std::ref(vec), 0, n/2);
std::thread t2(generarRandomVector, std::ref(vec), n/2, n);
t1.join();
t2.join();
Clock::time_point end = Clock::now();
std::cout << sec(end-start).count() << " seconds\n";
}
Don't use rand().
rand() uses a global state to generate the next number. Also some sources on the web [1] claim that it is 'thread-safe', that means that it may use a lock, thus serializing all the calls to rand() and eliminating all the concurrency.
There is no risk of data race with the code given above but a big risk of getting the same data in the two halve parts of the vector.
精彩评论