Skip to content

Instantly share code, notes, and snippets.

@MannyGozzi
Last active July 5, 2018 20:53
Show Gist options
  • Save MannyGozzi/96164c0ef401cbb83b2adca4a24d9eb0 to your computer and use it in GitHub Desktop.
Save MannyGozzi/96164c0ef401cbb83b2adca4a24d9eb0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
struct Timer { //use object lifetime to automatically print time elapsed
//within scope
//timepoints
std::chrono::time_point<std::chrono::steady_clock> begin, end;
//used to find change in time
std::chrono::duration<double> duration;
Timer()
{
//set begin time upon instantiation
begin = std::chrono::high_resolution_clock::now();
}
~Timer()
{
//find difference between beginning and end
end = std::chrono::high_resolution_clock::now();
duration = end - begin;
//std::fixed means keep the decimal point
//print time
std::cout << std::fixed << "Time elapsed: " << duration.count() << "\n";
}
};
int main()
{
{ //create a new scope so timer destructor is called
Timer timer;
std::cout << "Hello World" << std::endl;
} //when this curly brace is found the timer object is destroyed
std::cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment