-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeComplexityTest.cpp
More file actions
37 lines (33 loc) · 939 Bytes
/
TimeComplexityTest.cpp
File metadata and controls
37 lines (33 loc) · 939 Bytes
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
#include<iostream>
#include<chrono>
using namespace std;
using namespace chrono;
int slow(int n) {
if(n < 2)return n;
return slow(n - 1) + slow(n - 2);
}
int fast(int n) {
int *tmp = new int[n+5];
tmp[1] = tmp[2] = 1;
for(int i = 3; i <= n; i++) {
tmp[i] = tmp[i-1] + tmp[i-2];
tmp[i] %= 1000000000;
}
n = tmp[n];
delete[] tmp;
return n;
}
int main() {
for(int i = 30; i <= 55; i++) {
high_resolution_clock::time_point t1 = high_resolution_clock::now();
slow(i);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
fast(i);
high_resolution_clock::time_point t3 = high_resolution_clock::now();
duration<double> timeSpent = duration_cast<duration<double>>(t2 - t1);
cout << i << " " << timeSpent.count() << " ";
timeSpent = duration_cast<duration<double>>(t3 - t2);
cout << timeSpent.count() << endl;
}
return 0;
}