#include #include #include using namespace std; using namespace std::chrono; /** * Calculate the nth fibonacci recursively. * (Generally a very bad idea.) * @param n which fibonacci number to calculate. */ long fibonacci(int n); const int N1 = 46; const int N2 = 45; int main() { cout << "Calculating fibonacci(" << N1 << ") ... "; cout.flush(); auto start1 = steady_clock::now(); long result1 = fibonacci(N1); auto end1 = steady_clock::now(); double elapsed1 = duration_cast(end1 - start1).count(); cout << result1 << endl; cout << "Elapsed time = " << elapsed1/1000 << " seconds" << endl; cout << endl; cout << "Calculating fibonacci(" << N2 << ") ... "; cout.flush(); auto start2 = steady_clock::now(); long result2 = fibonacci(N2); auto end2 = steady_clock::now(); double elapsed2 = duration_cast(end2 - start2).count(); cout << result2 << endl; cout << "Elapsed time = " << elapsed2/1000 << " seconds" << endl; cout << endl; double total_elapsed = duration_cast(end2 - start1).count(); cout << "Total elapsed time = " << total_elapsed/1000 << " seconds" << endl; return 0; } long fibonacci(int n) { if (n <= 1) return n; else return fibonacci(n-2) + fibonacci(n-1); }