The following code results in very different times for g++ and clang++ when using uniform_real_distribution
.
#include <iostream>
#include <sstream>
#include <fstream>
#include <chrono>
#include <random>
std::mt19937::result_type seed = 0;
std::mt19937 gen(seed);
// std::uniform_int_distribution<size_t> distr(0, 1);
std::uniform_real_distribution<double> distr(0.0,1.0);
int main()
{
auto t_start = std::chrono::steady_clock::now();
for (auto i = 1; i <= 1000000; ++i)
{
distr(gen);
}
auto t_end = std::chrono::steady_clock::now();
std::cout << "elapsed time: " << std::chrono::duration_cast<std::chrono::nanoseconds>(t_end - t_start).count() << " ns\n" << std::endl;
return 0;
}
Compiled with the following commands:
clang++ -std=c++17 -O3 -flto -march=native -mllvm -inline-threshold=10000000 rng.cpp -o rng
g++ -std=c++17 -O3 -march=native rng.cpp -o rng
this results in the following times:
clang: 272929774 ns
gcc: 12054635 ns
when using the commented distribution instead, the times are:
clang: 48155862 ns
gcc: 50226810 ns
I have found a quite old question here which handles the same problem however none of the proposed solutions worked in my case.
Clang performance drop for specific C++ random number generation
Does someone has an idea what is going on here?