Advertisement
bartekltg

sort test

Feb 19th, 2024
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <algorithm>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. class timer{
  11.     std::chrono::time_point<std::chrono::high_resolution_clock> a,b;
  12. public:
  13.     void start(){a = std::chrono::high_resolution_clock::now();}
  14.     void stop() {b = std::chrono::high_resolution_clock::now();}
  15.     double value()
  16.     {
  17.         std::chrono::duration<double> elapsed_seconds = b-a;
  18.         return elapsed_seconds.count();
  19.     }
  20. };
  21.  
  22. int main()
  23. {
  24.     const size_t N = 120'000'000;
  25.     vector<uint64_t> tab;
  26.     random_device rd;
  27.     mt19937_64 gen(rd());
  28.  
  29.     uniform_int_distribution<uint64_t> dist(0);
  30.  
  31.  
  32.     for (int i=0; i<5; i++){
  33.         generate_n(back_inserter(tab), N, [&](){ return  dist(gen) ; } ) ;
  34.         timer st;
  35.         st.start();
  36.         sort(begin(tab),end(tab));
  37.         st.stop();
  38.         cout<<tab[3]<<endl; //do not optimize the array out, please
  39.         cout<<is_sorted(begin(tab),end(tab))<<endl;
  40.         cout<<st.value()*1000<<" ms"<<endl;
  41.         tab.clear();
  42.     }
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement