小编Alb*_*now的帖子

从第二次排列开始时极度减速

请考虑以下代码:

#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";
}
Run Code Online (Sandbox Code Playgroud)

-O2在我的AMD 4.1 GHz CPU上使用GCC(MinGW)5.3 进行编译2.3 s.但是,如果我在未注释的行中发表评论,它会减慢到3.4 s …

c++ performance function permutation compiler-optimization

8
推荐指数
1
解决办法
204
查看次数