fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <chrono>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <numeric>
  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. using namespace std;
  60.  
  61. const int Count = 1000000;
  62.  
  63. unsigned int Hovsepyan()
  64. {
  65. unsigned int sum = 0;
  66. for(int n = 0; n < Count; ++n)
  67. {
  68. n = std::abs(n);
  69. if (n < 10) { sum += n; continue; }
  70. std::stringstream io;
  71. io << n;
  72. char t;
  73. while (io >> t)
  74. sum += t - '0';
  75. }
  76. return sum;
  77. }
  78.  
  79. unsigned int olkhovich()
  80. {
  81. unsigned int sum = 0;
  82. for(int n = 0; n < Count; ++n)
  83. {
  84. int number = n;
  85. while (number) {
  86. sum += number % 10;
  87. number /= 10;
  88. }
  89. }
  90. return sum;
  91. }
  92.  
  93. unsigned int dIm0n()
  94. {
  95. unsigned int sum = 0;
  96. for(int n = 0; n < Count; ++n)
  97. {
  98. const auto s = to_string(n);
  99. sum += accumulate(cbegin(s), cend(s), 0,
  100. [](auto acc, auto x) {
  101. return acc + (x - '0');});
  102. }
  103. return sum;
  104. }
  105.  
  106. int main(int argc, const char * argv[])
  107. {
  108. {
  109. muTimer mt;
  110. unsigned int s = Hovsepyan();
  111. mt.stop();
  112. cout << "Hovsepyan() == " << s << " for " << mt.duration<>() << " mks\n";
  113. }
  114. {
  115. muTimer mt;
  116. unsigned int s = olkhovich();
  117. mt.stop();
  118. cout << "olkhovich() == " << s << " for " << mt.duration<>() << " mks\n";
  119. }
  120. {
  121. muTimer mt;
  122. unsigned int s = dIm0n();
  123. mt.stop();
  124. cout << "dIm0n() == " << s << " for " << mt.duration<>() << " mks\n";
  125. }
  126. }
  127.  
Success #stdin #stdout 0.52s 4464KB
stdin
Standard input is empty
stdout
Hovsepyan() == 27000000  for 434293 mks
olkhovich() == 27000000  for 6970 mks
dIm0n()     == 27000000  for 80521 mks