奇怪的 Visual Studio 行为:执行时间很长

Strange Visual Studio Behavior: Very long execution time

我注意到有些代码在 Visual Studio 中执行需要很长时间,而不是使用 CL 手动编译并 运行 执行可执行文件。

下面是展示此行为的代码示例:

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;
}

在VS2013中运行时,函数calc计算出答案大约需要13.5秒。在 VS2012 中,这下降到 3.5 秒。但是,当使用 CL(或我试过的任何其他编译器)手动编译时,可执行文件 returns 在 0.4 秒内给出答案。

如何解释这种差异,以及如何使 VS 执行与手册相同 compilation/execution?

谢谢。

问题是我 运行 使用 "Debug" 配置而不是 "Release"。将构建配置更改为 "Release" 已解决问题。有趣的是,它现在比手动运行快 4 倍 compilation/execution。

更新: 正如评论中指出的那样,很多减速是由于使用 std::min 和 std::max 函数造成的。切换到定制的 min/max 函数,执行速度提高了 3-4 倍。这里有一篇文章证实了这一观察:std::min causing three times slowdown