I'm trying to benchmark recursive fibonacci sequence calculator on C++. But surprisingly program outputs 0 nanoseconds, and start calculation after printing result. (CPU usage increase after printing 0 nanoseconds)
I think this is optimization feature of the compiler.
#include <iostream>
#include <chrono>
int fib2(int n) {
return (n < 2) ? n : fib2(n - 1) + fib2(n - 2);
}
int main(int argc, char* argv[])
{
auto tbegin = std::chrono::high_resolution_clock::now();
int a = fib2(50);
auto tend = std::chrono::high_resolution_clock::now();
std::cout << (tend - tbegin).count() << " nanoseconds" << std::endl;
std::cout << "fib => " << a << std::endl;
}
Output:
0 nanoseconds
Is it feature? If yes, how can I disable this feature?