-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.cpp
74 lines (62 loc) · 2.08 KB
/
fibonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file fibonacci.cpp
* @brief Fibonacci sequence implementation performance comparison
* @details Implementation of the Fibonacci sequence using both recursion and iteration
* https://oeis.org/A000045
*/
#include <iostream>
#include <vector>
#include <chrono>
/**
* @brief Fibonacci sequence implementation using recursion
* @param n The nth number in the Fibonacci sequence to calculate
* @return The nth number in the Fibonacci sequence
*/
uint64_t fibonacci_recursion(uint64_t n)
{
if (n <= 1) {
return n;
}
return fibonacci_recursion(n - 1) + fibonacci_recursion(n - 2);
}
/**
* @brief Fibonacci sequence implementation using iteration
* @param n The nth number in the Fibonacci sequence to calculate
* @return The nth number in the Fibonacci sequence
*/
uint64_t fibonacci_iteration(int n)
{
std::vector<uint64_t> fib(n + 1);
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; ++i) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
#include <iostream>
#include <vector>
#include <cstdlib>
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <n>" << std::endl;
return 1;
}
int n = std::atoi(argv[1]);
if (n < 0) {
std::cerr << "n must be a positive integer" << std::endl;
return 1;
}
// Use chrono to time the execution of each function
auto start = std::chrono::high_resolution_clock::now();
auto result = fibonacci_recursion(n);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Fibonacci recursion: " << result << " (" << duration.count() << " microseconds)" << std::endl;
start = std::chrono::high_resolution_clock::now();
result = fibonacci_iteration(n);
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Fibonacci iteration: " << result << " (" << duration.count() << " microseconds)" << std::endl;
return 0;
}