-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib1.cpp
More file actions
81 lines (70 loc) · 1.47 KB
/
Copy pathfib1.cpp
File metadata and controls
81 lines (70 loc) · 1.47 KB
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
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "math.h"
int fib1(int n){
if (n == 0){
return 0;
}
if (n == 1){
return 1;
}
return fib1(n-1) + fib1(n-2);
}
int fib2(int n){
if (n == 0){
return 0;
}
std::vector<int> v;
int f0 = 0;
int f1 = 1;
int fn;
v.push_back(f0);
v.push_back(f1);
for(int i = 2; i < n; i++){
fn = v[i-1] + v[i-2];
v.push_back(fn);
}
return v[n];
}
int nSquared(int n){
return n*n;
}
int nCubed(int n){
return n*n*n;
}
int nLogn(int n){
return n * log(n);
}
int main( ) {
std::srand( std::time( 0 ) );
int reps = 1000000000;
for(int i = 0; i < 41; i++){
int N = i;
// clock_t t0 = std::clock( );
// for(int i = 0; i < reps; i++){
// fib1( N);
// }
// clock_t t1 = std::clock( );
// double dt = ((double)(t1 - t0)) / CLOCKS_PER_SEC;
// std::cout <<"fib1: " << N << "," << dt/reps << std::endl;
// clock_t t2 = std::clock( );
// for(int i = 0; i < reps; i++){
// fib2(N);
// }
//
// clock_t t3 = std::clock( );
// double dt2 = ((double)(t3 - t2)) / CLOCKS_PER_SEC;
// std::cout << "fib2: "<< N << "," << dt2/reps << std::endl;
clock_t t0 = std::clock( );
for(int i = 0; i < reps; i++){
nLogn( N);
}
clock_t t1 = std::clock( );
double dt = ((double)(t1 - t0)) / CLOCKS_PER_SEC;
std::cout <<"fib1: " << N << "," << dt/reps << std::endl;
}
return 0;
}