[英]Using ISO C++0x threads degrades performance
I'm writing a program in C++ using threads to improve the efficiency. 我正在使用线程编写C ++程序,以提高效率。
Basically I'm just creating a huge vector of integers (1 GB) and filling it with random numbers. 基本上,我只是创建一个巨大的整数向量(1 GB),并用随机数填充它。 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. 现在,我想使用2个线程并看到时间的改善,但是使用2个线程的程序比不使用2个线程的时间要多得多。 Don't know what I'm doing wrong :S
不知道我在做什么错: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. 如果需要,我还可以发布
generateRandomVector
函数。
Hope someone can help :D 希望有人可以帮助:D
EDIT - generateRandomVector
function: 编辑-
generateRandomVector
功能:
void generarRandomVector(vector<int> &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>
: 这是使用
<random>
和<chrono>
的完整C ++ 0x解决方案:
#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()。
rand() uses a global state to generate the next number. rand()使用全局状态生成下一个数字。 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.
网上[1]上的一些消息来源还声称它是“线程安全的”,这意味着它可以使用锁,从而将对rand()的所有调用序列化,并消除了所有并发性。
上面给出的代码没有数据竞争的风险,但是在向量的两半部分中获得相同数据的风险很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.