Umgang mit Zeiten in C++
Für Microbenchmarks besser eine Bibliothek verwenden und Fachtexte lesen, aber hier ist ein einfaches Beispiel:
#include <iostream>
#include <ostream>
#include <string>
#include <chrono>
using namespace ::std::literals;
typedef ::std::chrono::high_resolution_clock Clock;
typedef ::std::chrono::duration<double, std::micro> microseconds ;
typedef ::Clock::time_point time_point;
int fib( int const a ){ return a == 0 ? 0 : a == 1 ? 1 : fib( a - 1 )+ fib( a - 2 ); }
void test( int const a )
{ ::time_point start = ::Clock::now();
auto const f = fib( a );
auto const duration = ::std::chrono::duration_cast< microseconds >
( Clock::now() - start ).count();
::std::cout << a << ' ' << f << ' ' << duration << '\n'; }
int main()
{ for( int j = 1; j < 3; ++j )for( int i = 1; i < 12; ++i )test( i ); }