0

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.

  • 1
    Did you maybe forgot to switch to release? When I copy-paste this into VS 2019 and run as release build I get 3122 :) – Lukas-T Dec 09 '20 at 19:11
  • @churill Oh, you're right, this work to my problem. I really appreciate that. But what's difference between debug and release? Before I post this question, I had tried executing the .exe file in the debug direction, but it still has the same performance problem. I think I need to study for this. At last, thanks for your answer again. – Cedric Vincent Dec 09 '20 at 19:22
  • 1
    Release builds are optimized in various ways. For example the compiler might inline short functions, eliminate temporary variables, reorder function calls, etc, as long as the observable behaviour doesn't change. Obviously it's hard to find bugs in code when the compiler changed it completely, that's what the debug-build is for. It's several times slower, but contains all the information to debug it ;) – Lukas-T Dec 09 '20 at 19:46
  • Ok, got it. It is so nice of you. Wish you the best of everything in the future. :) – Cedric Vincent Dec 09 '20 at 19:52

1 Answers1

0

First, thanks for @churill commented. His advice is very useful.

There are a lot differences between Debug and Release.

The key factor of influence to this problem is that Release will optimize when compiling.

For more information:

https://stackoverflow.com/a/938665/6367757

medium article

  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Jeaninez - MSFT Dec 15 '20 at 07:58