Can someone explain to me why second for loop, with old rand(), executes a lot faster than the first loop? I have seen through multiple post here that using c++11 engine and uniform distribution are being recommended bacause rand() + multithreading is a perf bottleneck? Also as suggested I have generator and function per thread.
int main(int argc, char *argv[])
{
std::vector<mt19937> generators(omp_get_max_threads());
std::vector<uniform_real_distribution<double>> functions(omp_get_max_threads());
for (int i = 0; i < omp_get_max_threads(); i++)
{
// seed is random
generators[i] = mt19937(1654 + 17*i);
functions[i] = uniform_real_distribution<double>(0.0,1.0);
}
double itime = omp_get_wtime();
#pragma omp parallel for
for (int i =0; i < 10000000; i++)
{
int a = functions[omp_get_thread_num()](generators[omp_get_thread_num()]);
}
double end = omp_get_wtime() - itime;
srand(time(NULL));
double start = omp_get_wtime();
#pragma omp parallel for
for (int i =0; i < 10000000; i++)
{
int a = ((double) rand() / (RAND_MAX));
}
double endR = omp_get_wtime() - start;
cout << "Generator: " << end<<endl;
cout<<"Old: "<<endR << endl;
}