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