fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  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.  
  60. int main(int argc, const char * argv[])
  61. {
  62. const int N = 10000000;
  63. {
  64. muTimer mu;
  65. double sum = 0;
  66. for(int i = 1; i <= N; ++i)
  67. {
  68. double x = 200/double(i);
  69. sum += x;
  70. }
  71. mu.stop();
  72. cout << mu.duration() << endl;
  73. cout << sum << endl;
  74. }
  75. {
  76. muTimer mu;
  77. double sum = 0;
  78. for(int i = 1; i <= N; ++i)
  79. {
  80. double * f = new double(i);
  81. double x = 200/double(*f);
  82. sum += x;
  83. delete f;
  84. }
  85. mu.stop();
  86. cout << mu.duration() << endl;
  87. cout << sum << endl;
  88. }
  89. }
  90.  
Success #stdin #stdout 0.3s 15232KB
stdin
Standard input is empty
stdout
36562
3339.06
262213
3339.06