考虑以下代码:
#include <algorithm>
#include <chrono>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v(12);
std::iota(v.begin(), v.end(), 0);
//std::next_permutation(v.begin(), v.end());
using clock = std::chrono::high_resolution_clock;
clock c;
auto start = c.now();
unsigned long counter = 0;
do {
++counter;
} while (std::next_permutation(v.begin(), v.end()));
auto end = c.now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << counter << " permutations took " << duration.count() / 1000.0f << " s";
}
在我的 AMD 4.1 GHz CPU 上使用 GCC (MinGW) 5.3 -O2
编译,这需要 2.3 s
。但是,如果我在未注释的行中添加注释,它会减慢到 3.4 s
。我希望有一个最小的加速,因为我们测量一个排列的时间更少。使用 -O3
时,差异不那么极端 2.0 s
到 2.4 s
。
谁能解释一下? super 智能的编译器能否检测到我想要遍历所有排列并优化此代码?
最佳答案
我认为编译器会因为您在代码中的两行中调用该函数而感到困惑,导致它不是内联的。
GCC 8.0.0 的行为也与您的一样。
Benefits of inline functions in C++?它为编译器提供了一种简单的机制来应用更多优化,因此在某些情况下,丢失内联标识可能会导致性能严重下降。
关于c++ - 从第二个排列开始时极度减速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749074/