fork(2) download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <chrono>
  6. #include <random>
  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. int day(const char * nm)
  60. {
  61. long long x = nm[0]+nm[1]+nm[2]-300;
  62. return (((((101047ll*x+694384ll)*x-19207693ll)*x-61935874ll)*x
  63. +1060034136ll)*x-1218412800)*(2+x)/2058376320+1;
  64. }
  65.  
  66. int DAY(const char * str)
  67. {
  68. if((str[0] =='M') && (str[1] == 'o') && (str[2] == 'n')) return 1;
  69. if((str[0] =='T') && (str[1] == 'u') && (str[2] == 'e')) return 2;
  70. if((str[0] =='W') && (str[1] == 'e') && (str[2] == 'd')) return 3;
  71. if((str[0] =='T') && (str[1] == 'h') && (str[2] == 'u')) return 4;
  72. if((str[0] =='F') && (str[1] == 'r') && (str[2] == 'i')) return 5;
  73. if((str[0] =='S') && (str[1] == 'a') && (str[2] == 't')) return 6;
  74. if((str[0] =='S') && (str[1] == 'u') && (str[2] == 'n')) return 7;
  75. return 0;
  76. }
  77.  
  78.  
  79. int main()
  80. {
  81.  
  82. char M[7][4] = { "Mon","Tue","Wed","Thu","Fri","Sat","Sun" };
  83.  
  84. {
  85. mt19937 gen(0);
  86. uniform_int_distribution<> distrib(0,6);
  87. int sum = 0;
  88. muTimer mt;
  89. for(int i = 0; i < 20000000; ++i)
  90. {
  91. sum += day(M[distrib(gen)]);
  92. }
  93. mt.stop();
  94. cout << sum << " for " << mt.duration<>() << " mks\n";
  95. }
  96. {
  97. mt19937 gen(0);
  98. uniform_int_distribution<> distrib(0,6);
  99. int sum = 0;
  100. muTimer mt;
  101. for(int i = 0; i < 20000000; ++i)
  102. {
  103. sum += DAY(M[distrib(gen)]);
  104. }
  105. mt.stop();
  106. cout << sum << " for " << mt.duration<>() << " mks\n";
  107. }
  108.  
  109. }
  110.  
Success #stdin #stdout 1.34s 4504KB
stdin
Standard input is empty
stdout
80000618 for 587948 mks
80000618 for 747356 mks