简体   繁体   中英

Strange Visual Studio Behavior: Very long execution time

I have noticed that some code takes a very long time to execute in Visual Studio as opposed to compiling it manually with CL and running the executable.

Here is an example of code which exhibits this behavior:

int DP[MAX][MAX];
class CartInSupermarketEasy {
public:
int calc(int N, int K) {
    clock_t begin = clock();
    for (int i = 0; i < MAX; ++i) {
        DP[0][i] = 0;
        DP[1][i] = 1;
        DP[i][0] = i;
    }

    for (int n = 1; n <= N; ++n) {
        for (int k = 0; k <= K; ++k) {
            int min_res = N;
            for (int i = 1; i < n; ++i) {
                for (int j = 0; j < k; ++j) {
                    int curr_res = max(DP[n - i][k - 1 - j], DP[i][j]) + 1;
                    min_res = min(curr_res, min_res);
                }
            }
            DP[n][k] = min(min_res, DP[n - 1][k] + 1);
        }
    }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << '\n';
    return DP[N][K];
    }
} CI;

int main(){
    cout << CI.calc(100,100) << '\n';
    return 0;
}

When run in VS2013, the function calc takes about 13.5 seconds to calculate the answer. In VS2012, this goes down to 3.5 seconds. However, when compiled manually with CL (or any other compiler that I've tried ), the executable returns an answer in .4 seconds.

How can this discrepancy be explained, and how can I make VS execute on par with manual compilation/execution?

Thank you.

The problem was that I was running using the "Debug" configuration instead of "Release". Changing it the build configuration to "Release" has solved the problem. Interestingly, it runs about 4 times faster than manual compilation/execution now.

Update: As pointed out in the comments, a lot of the slowdown was caused due to use of std::min and std::max functions. Switching to custom made min/max functions, sped up the execution by a factor of 3-4. Here is an article confirming this observation: std::min causing three times slowdown

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM