Last active
July 5, 2018 20:53
-
-
Save MannyGozzi/96164c0ef401cbb83b2adca4a24d9eb0 to your computer and use it in GitHub Desktop.
This file contains 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
#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