fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <regex>
  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. regex rg("-{0,1}\\d{11}");
  59.  
  60. inline int match_r(const char * s)
  61. {
  62. return regex_match(s,rg);
  63. }
  64.  
  65. inline int match_p(const char * s)
  66. {
  67. if (*s == '-') ++s;
  68. for(int i = 0; i < 11 ; ++i, ++s)
  69. if (*s > '9' || *s < '0') return 0;
  70. return *s == 0;
  71. }
  72.  
  73. int main(int argc, char * argv[])
  74. {
  75. vector<char*> v;
  76. random_device r;
  77. default_random_engine e(r());
  78. uniform_int_distribution<long long> dist(10000000000,99999999999);
  79. const int N = 1'000'000;
  80. for(int i = 0; i < N; ++i)
  81. {
  82. char * s = new char[20];
  83. long long L = dist(e);
  84. if (i%2) L = -L;
  85. sprintf(s,"%lld",L);
  86. v.push_back(s);
  87. s = new char[20];
  88. sprintf(s,"%lld",L);
  89. switch(N%3)
  90. {
  91. case 0: s[dist(e)%8+2] = 0; break;
  92. case 1: strcat(s,"123"); break;
  93. case 2: s[dist(e)%8+2] = 'a' + dist(e)%8+2; break;
  94. }
  95. v.push_back(s);
  96. }
  97.  
  98. {
  99. muTimer mt;
  100. int cnt = 0;
  101. for(auto s: v) cnt += match_r(s);
  102. mt.stop();
  103. cout << cnt << " for " << mt.duration<>() << " mks\n";
  104. }
  105. {
  106. muTimer mt;
  107. int cnt = 0;
  108. for(auto s: v) cnt += match_p(s);
  109. mt.stop();
  110. cout << cnt << " for " << mt.duration<>() << " mks\n";
  111. }
  112.  
  113. }
  114.  
Success #stdin #stdout 0.79s 81492KB
stdin
Standard input is empty
stdout
1000000 for 493736 mks
1000000 for 18094 mks