-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cc
More file actions
43 lines (31 loc) · 847 Bytes
/
Copy pathfibonacci.cc
File metadata and controls
43 lines (31 loc) · 847 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
38
39
40
41
42
43
#include <iostream>
#include <chrono>
int fib( int n ) {
if ( n < 1 ) return 0;
if ( n < 3 ) return 1;
return fib( n - 2 ) + fib( n - 1 );
}
int fib2( int n ) {
if ( n < 1 ) return 0;
int a = 1;
int b = 1;
for( int i = 3 ; i <= n; i++ ) {
int ab = a+b;
a = b;
b = ab;
}
return b;
}
void run_fib( int (*f) (int) ) {
auto start = std::chrono::high_resolution_clock::now();
std::cout << f( 1 ) << std::endl;
std::cout << f( 5 ) << std::endl;
std::cout << f( 40 ) << std::endl;
auto end = std::chrono::high_resolution_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>( end-start );
std::cout << "elapsed: " << diff.count() << std::endl;
}
int main() {
run_fib(fib);
run_fib(fib2);
}