fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <chrono>
  4.  
  5. using namespace std;
  6.  
  7. class muTimer
  8. {
  9. using Clock = std::chrono::high_resolution_clock;
  10. bool active = false;
  11. Clock::duration duration_;
  12. Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
  13.  
  14. muTimer(const muTimer&) = delete;
  15. muTimer& operator=(const muTimer&) = delete;
  16. public:
  17. using ns = std::chrono::nanoseconds;
  18. using mks = std::chrono::microseconds;
  19. using ms = std::chrono::milliseconds;
  20. muTimer() { reset(); start(); }
  21. ~muTimer() = default;
  22. muTimer& reset()
  23. {
  24. duration_ = std::chrono::nanoseconds(0);
  25. active = false;
  26. return *this;
  27. }
  28. muTimer& start()
  29. {
  30. if (!active)
  31. {
  32. start_ = Clock::now();
  33. active = true;
  34. }
  35. return *this;
  36. }
  37. muTimer& stop()
  38. {
  39. if (active)
  40. {
  41. stop_ = Clock::now();
  42. duration_ += stop_ - start_;
  43. active = false;
  44. }
  45. return *this;
  46. }
  47. template<typename T = mks>
  48. unsigned long long duration()
  49. {
  50. return static_cast<unsigned long long>
  51. (std::chrono::duration_cast<T>(stop_-start_).count());
  52. }
  53. };
  54.  
  55. long long fibR(unsigned int N)
  56. {
  57. if (N < 2) return 1;
  58. return fibR(N-1) + fibR(N-2);
  59. }
  60.  
  61. long long fibI(unsigned int N)
  62. {
  63. if (N < 2) return 1;
  64. long long f0 = 1, f1 = 1;
  65. for(unsigned int i = 2; i <= N; ++i)
  66. {
  67. long long f = f0 + f1;
  68. f0 = f1;
  69. f1 = f;
  70. }
  71. return f1;
  72. }
  73.  
  74. int main(int argc, const char * argv[])
  75. {
  76. for(unsigned int N = 3; N < 35; N += 3)
  77. {
  78. unsigned long long F;
  79. {
  80. muTimer mt;
  81. for(int i = 0; i < 100; ++i)
  82. F = fibI(N);
  83. unsigned long long t = mt.stop().duration();
  84. cout << "N(i) = " << setw(3) << N << setw(10) << F << setw(10) << t << " mks\n";
  85. }
  86. {
  87. muTimer mt;
  88. for(int i = 0; i < 100; ++i)
  89. F = fibR(N);
  90. unsigned long long t = mt.stop().duration();
  91. cout << "N(r) = " << setw(3) << N << setw(10) << F << setw(10) << t << " mks\n";
  92. }
  93. }
  94. }
  95.  
  96.  
Success #stdin #stdout 1.42s 4532KB
stdin
Standard input is empty
stdout
N(i) =   3         3         0 mks
N(r) =   3         3         1 mks
N(i) =   6        13         0 mks
N(r) =   6        13         5 mks
N(i) =   9        55         1 mks
N(r) =   9        55        22 mks
N(i) =  12       233         2 mks
N(r) =  12       233       123 mks
N(i) =  15       987         2 mks
N(r) =  15       987       397 mks
N(i) =  18      4181         3 mks
N(r) =  18      4181      1710 mks
N(i) =  21     17711         3 mks
N(r) =  21     17711      7261 mks
N(i) =  24     75025         4 mks
N(r) =  24     75025     27304 mks
N(i) =  27    317811         3 mks
N(r) =  27    317811     91201 mks
N(i) =  30   1346269         3 mks
N(r) =  30   1346269    257996 mks
N(i) =  33   5702887         2 mks
N(r) =  33   5702887   1025391 mks