#include <iostream>
#include <iomanip>
#include <chrono>
using namespace std;
class muTimer
{
using Clock = std::chrono::high_resolution_clock;
bool active = false;
Clock::duration duration_;
Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
muTimer(const muTimer&) = delete;
muTimer& operator=(const muTimer&) = delete;
public:
using ns = std::chrono::nanoseconds;
using mks = std::chrono::microseconds;
using ms = std::chrono::milliseconds;
muTimer() { reset(); start(); }
~muTimer() = default;
muTimer& reset()
{
duration_ = std::chrono::nanoseconds(0);
active = false;
return *this;
}
muTimer& start()
{
if (!active)
{
start_ = Clock::now();
active = true;
}
return *this;
}
muTimer& stop()
{
if (active)
{
stop_ = Clock::now();
duration_ += stop_ - start_;
active = false;
}
return *this;
}
template<typename T = mks>
unsigned long long duration()
{
return static_cast<unsigned long long>
(std::chrono::duration_cast<T>(stop_-start_).count());
}
};
long long fibR(unsigned int N)
{
if (N < 2) return 1;
return fibR(N-1) + fibR(N-2);
}
long long fibI(unsigned int N)
{
if (N < 2) return 1;
long long f0 = 1, f1 = 1;
for(unsigned int i = 2; i <= N; ++i)
{
long long f = f0 + f1;
f0 = f1;
f1 = f;
}
return f1;
}
int main(int argc, const char * argv[])
{
for(unsigned int N = 3; N < 35; N += 3)
{
unsigned long long F;
{
muTimer mt;
for(int i = 0; i < 100; ++i)
F = fibI(N);
unsigned long long t = mt.stop().duration();
cout << "N(i) = " << setw(3) << N << setw(10) << F << setw(10) << t << " mks\n";
}
{
muTimer mt;
for(int i = 0; i < 100; ++i)
F = fibR(N);
unsigned long long t = mt.stop().duration();
cout << "N(r) = " << setw(3) << N << setw(10) << F << setw(10) << t << " mks\n";
}
}
}