-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfibonacci-sequence-cache-vs-computation.cpp
88 lines (71 loc) · 1.89 KB
/
fibonacci-sequence-cache-vs-computation.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* @file fibonacci-sequence.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-14
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <chrono>
using namespace std;
int fib[500];
int fib_recursive(int n) {
if (n <= 1) {
return n;
}
return fib_recursive(n-1) + fib_recursive(n-2);
}
int fib_dp(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (fib[n] != 0) {
return fib[n];
}
fib[n] = fib_dp(n-1) + fib_dp(n-2);
return fib[n];
}
int fib_ultimate(int n) {
int i;
int back2 = 0,back1 = 1;
int next;
if(n==0) return 0;
for(i=2;i<n;i++) {
next = back2 + back1;
back2 = back1;
back1 = next;
}
return next;
}
int main(int argc, const char** argv) {
int n;
n = 30;
using milli = std::chrono::milliseconds;
auto start = std::chrono::high_resolution_clock::now();
fib_recursive(n);
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "recursion took "
<< std::chrono::duration_cast<milli>(finish - start).count()
<< " milliseconds\n";
using milli = std::chrono::milliseconds;
start = std::chrono::high_resolution_clock::now();
fib_dp(n);
finish = std::chrono::high_resolution_clock::now();
std::cout << "dp took "
<< std::chrono::duration_cast<milli>(finish - start).count()
<< " milliseconds\n";
using milli = std::chrono::milliseconds;
start = std::chrono::high_resolution_clock::now();
fib_ultimate(n);
finish = std::chrono::high_resolution_clock::now();
std::cout << "dp with lessa space took "
<< std::chrono::duration_cast<milli>(finish - start).count()
<< " milliseconds\n";
return 0;
}