Created
October 30, 2017 23:55
-
-
Save gchavez2/1fb558c7a0e0a3cd5b0c8a8a3966a3a5 to your computer and use it in GitHub Desktop.
Chrono timers C++11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -std=c++11 testTimer.cpp -o testTimer.out | |
// ./testTimer.out | |
#include <iostream> | |
#include <chrono> | |
#include <ctime> | |
long fibonacci(unsigned n) | |
{ | |
if (n < 2) return n; | |
return fibonacci(n-1) + fibonacci(n-2); | |
} | |
int main() | |
{ | |
auto start = std::chrono::system_clock::now(); | |
std::cout << "f(42) = " << fibonacci(42) << '\n'; | |
auto end = std::chrono::system_clock::now(); | |
std::chrono::duration<double> elapsed_seconds = end-start; | |
std::time_t end_time = std::chrono::system_clock::to_time_t(end); | |
std::cout << "finished computation at " << std::ctime(&end_time) | |
<< "elapsed time: " << elapsed_seconds.count() << "s\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment