zoukankan      html  css  js  c++  java
  • C++17并行STL

    尝试了一下C++17的并行STL排序,速度提升比较明显。

    环境是VS2019。

    #include <algorithm>
    #include <execution>
    #include <iostream>
    #include <random>
    #include <chrono>   
    
    using namespace std;
    using namespace chrono;
    
    int main() 
    {
        vector<long long> d1(30000000);
        vector<long long> d2(30000000);
    
        mt19937 gen;
        uniform_int_distribution<long long> dis(0, 100000000);
        auto rand_num([=]() mutable { return dis(gen); });
    
        generate(execution::par, begin(d1), end(d1), rand_num);
        d2 = d1;
        
        auto start_t = high_resolution_clock::now();
        sort(begin(d1), end(d1));
        auto end_t = high_resolution_clock::now();
        auto duration = duration_cast<nanoseconds>(end_t - start_t);
        cout << "The run time is: " << double(duration.count()) * nanoseconds::period::num / nanoseconds::period::den << "s" << endl;
    
        start_t = high_resolution_clock::now();
        sort(execution::par, begin(d2), end(d2));
        end_t = high_resolution_clock::now();
        duration = duration_cast<nanoseconds>(end_t - start_t);
        cout << "The run time is: " << double(duration.count()) * nanoseconds::period::num / nanoseconds::period::den << "s" << endl;
       
        return 0;
    }

    速度对比:

  • 相关阅读:
    项目报错二
    项目报错一
    OCP-1Z0-051-V9.02-21题
    Windows API——OpenClipboard——剪切板
    如果在CEdit中实现Ctrl+V、Ctrl+C、Ctrl+X的功能
    OCP-1Z0-051-V9.02-18题
    OCP-1Z0-051-V9.02-17题
    OCP-1Z0-051-V9.02-15题
    OCP-1Z0-051-V9.02-14题
    OCP-1Z0-051-V9.02-12题
  • 原文地址:https://www.cnblogs.com/tiandsp/p/14231385.html
Copyright © 2011-2022 走看看