-
Notifications
You must be signed in to change notification settings - Fork 2
/
constexpr_fib.cpp
41 lines (31 loc) · 932 Bytes
/
constexpr_fib.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
#include <iostream>
#include <chrono>
using namespace std;
class Timer{
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
public:
Timer() : m_beg(clock_t::now()){}
void reset(){
m_beg = clock_t::now();
}
double elapsed() const{
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
};
constexpr long int fib(int num){
return num>=1 ? fib(num-1) + fib(num-2) : 1;
}
int main(int argc, char const *argv[]){
Timer timer;
const long int result = fib(200);
// const long int num1 = fib(99);
// const long int num2 = fib(98);
// fprintf(stdout, "fib(99) = %ld\tfib(98) = %ld\nfib(100) = %ld\tsummation is %ld\n",
// num1, num2, result, num1+num2);
cout << "Time relapsed: " << timer.elapsed() << endl;
cout << "Fibonacci value: " << result << endl;
return 0;
}