Skip to content

Instantly share code, notes, and snippets.

@mcleary
Created October 14, 2016 14:37
Show Gist options
  • Save mcleary/1bf76fae686ce8d9c21bbad9167085f2 to your computer and use it in GitHub Desktop.
Save mcleary/1bf76fae686ce8d9c21bbad9167085f2 to your computer and use it in GitHub Desktop.
Timing a function with <chrono>
#include <iostream>
#include <thread>
#include <chrono>
void really_long_function()
{
using namespace std::chrono;
// Very cool user defined literals for duration representation
std::this_thread::sleep_for(3000ms);
}
int main(int argc, const char * argv[])
{
using namespace std::chrono;
auto begin_time = steady_clock::now();
really_long_function();
auto end_time = steady_clock::now();
auto function_time = duration_cast<milliseconds>(end_time - begin_time);
std::cout << function_time.count() << "ms" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment