fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <chrono>
  8.  
  9. class muTimer
  10. {
  11. using Clock = std::chrono::high_resolution_clock;
  12. bool active = false;
  13. Clock::duration duration_;
  14. Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
  15.  
  16. muTimer(const muTimer&) = delete;
  17. muTimer& operator=(const muTimer&) = delete;
  18. public:
  19. using ns = std::chrono::nanoseconds;
  20. using mks = std::chrono::microseconds;
  21. using ms = std::chrono::milliseconds;
  22. muTimer() { reset(); start(); }
  23. ~muTimer() = default;
  24. muTimer& reset()
  25. {
  26. duration_ = std::chrono::nanoseconds(0);
  27. active = false;
  28. return *this;
  29. }
  30. muTimer& start()
  31. {
  32. if (!active)
  33. {
  34. start_ = Clock::now();
  35. active = true;
  36. }
  37. return *this;
  38. }
  39. muTimer& stop()
  40. {
  41. if (active)
  42. {
  43. stop_ = Clock::now();
  44. duration_ += stop_ - start_;
  45. active = false;
  46. }
  47. return *this;
  48. }
  49. template<typename T = mks>
  50. unsigned long long duration()
  51. {
  52. return static_cast<unsigned long long>
  53. (std::chrono::duration_cast<T>(stop_-start_).count());
  54. }
  55. };
  56.  
  57. using namespace std;
  58.  
  59.  
  60. int main()
  61. {
  62. const int N = 1000;
  63. vector<int> y(N), z(N);
  64. for(int i = 0; i < N; ++i)
  65. y[i] = z[i] = rand()%200;
  66.  
  67. {
  68. muTimer mt;
  69. sort(y.begin(),y.end());
  70. mt.stop();
  71. cout << "Sorting : " << mt.duration<>() << " mks\n";
  72. }
  73. {
  74. muTimer mt;
  75. auto p = partition(z.begin(),z.end(),[](int x) { return x < 50; });
  76. p = partition(p,z.end(),[](int x) { return x < 100; });
  77. p = partition(p,z.end(),[](int x) { return x < 150; });
  78. mt.stop();
  79. cout << "Partition: " << mt.duration<>() << " mks\n";
  80. }
  81.  
  82. cout << endl;
  83.  
  84. auto p1 = partition_point(y.begin(),y.end(),[](int x) { return x < 50; });
  85. auto p2 = partition_point(z.begin(),z.end(),[](int x) { return x < 50; });
  86.  
  87. cout << p1 - y.begin() << " vs " << p2 - z.begin() << endl;
  88.  
  89. p1 = partition_point(y.begin(),y.end(),[](int x) { return x < 100; });
  90. p2 = partition_point(z.begin(),z.end(),[](int x) { return x < 100; });
  91.  
  92. cout << p1 - y.begin() << " vs " << p2 - z.begin() << endl;
  93.  
  94. p1 = partition_point(y.begin(),y.end(),[](int x) { return x < 150; });
  95. p2 = partition_point(z.begin(),z.end(),[](int x) { return x < 150; });
  96.  
  97. cout << p1 - y.begin() << " vs " << p2 - z.begin() << endl;
  98.  
  99. }
  100.  
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
Sorting  : 32 mks
Partition: 6 mks

242 vs 242
484 vs 484
735 vs 735