I try to transplant project from Code::Blocks to Visual C++ 2019. After that, I am undergoing some performance problems.
The most considerable performance dropping is that when I need to generate large amount of random double variables.
After Searching for information from the internet, I still can't give a reasonable explain for this.
I briefly extract part of my code below:
#include <iostream>
#include <random>
#include <time.h>
class PublicFunction {
public:
static double RandomDouble(double, double);
private:
static std::mt19937 generator_;
};
std::mt19937 PublicFunction::generator_(static_cast<int>(time(0)));
double PublicFunction::RandomDouble(double min_value, double max_value) {
std::uniform_real_distribution<double> distribution(min_value, max_value);
return distribution(PublicFunction::generator_);
}
int main() {
double d;
double t_start, t_end;
t_start = clock();
for (unsigned i = 0; i < 100000000; ++i) {
d = PublicFunction::RandomDouble(-100, 100);
}
t_end = clock();
std::cout << (t_end - t_start) << std::endl;
system("PAUSE");
return 0;
}
After running the code in code::blocks which is using C++ 14 and in Visual C++ 2019.
code::blocks finished with execution time: 6155 ms, whereas Visual C++ got 40305 ms, it is almost 7 times slower.
I'm wondering why this problem happened, and is there any way to fix it? Thanks.