fork(1) download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <list>
  6. #include <chrono>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. class muTimer
  12. {
  13. using Clock = std::chrono::high_resolution_clock;
  14. bool active = false;
  15. Clock::duration duration_;
  16. Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
  17.  
  18. muTimer(const muTimer&) = delete;
  19. muTimer& operator=(const muTimer&) = delete;
  20. public:
  21. using ns = std::chrono::nanoseconds;
  22. using mks = std::chrono::microseconds;
  23. using ms = std::chrono::milliseconds;
  24. muTimer() { reset(); start(); }
  25. ~muTimer() = default;
  26. muTimer& reset()
  27. {
  28. duration_ = std::chrono::nanoseconds(0);
  29. active = false;
  30. return *this;
  31. }
  32. muTimer& start()
  33. {
  34. if (!active)
  35. {
  36. start_ = Clock::now();
  37. active = true;
  38. }
  39. return *this;
  40. }
  41. muTimer& stop()
  42. {
  43. if (active)
  44. {
  45. stop_ = Clock::now();
  46. duration_ += stop_ - start_;
  47. active = false;
  48. }
  49. return *this;
  50. }
  51. template<typename T = mks>
  52. unsigned long long duration()
  53. {
  54. return static_cast<unsigned long long>
  55. (std::chrono::duration_cast<T>(stop_-start_).count());
  56. }
  57. };
  58.  
  59.  
  60. list<int> L;
  61. vector<int> V;
  62.  
  63. int main(int argc, const char * argv[])
  64. {
  65. for(int i = 0; i < 1000000; ++i)
  66. {
  67. int r = rand();
  68. V.push_back(r);
  69. L.push_back(r);
  70. }
  71. L.sort();
  72. sort(V.begin(),V.end());
  73.  
  74. {
  75. muTimer mu;
  76. long long int sum = 0;
  77. for(int i: V) sum += i;
  78. mu.stop();
  79. cout << "Sum " << sum << " for " << mu.duration() << " mks\n";
  80. }
  81. {
  82. muTimer mu;
  83. long long int sum = 0;
  84. for(int i: L) sum += i;
  85. mu.stop();
  86. cout << "Sum " << sum << " for " << mu.duration() << " mks\n";
  87. }
  88.  
  89. }
  90.  
Success #stdin #stdout 0.57s 46392KB
stdin
Standard input is empty
stdout
Sum 1073756018481283 for 456 mks
Sum 1073756018481283 for 80300 mks