Last active
October 23, 2020 16:05
-
-
Save wojexe/7366344ec685ddd6f2cda2bcfd9db28b to your computer and use it in GitHub Desktop.
C++ block execution timer
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 <chrono> | |
#include <iostream> | |
class Timer { | |
std::chrono::high_resolution_clock::time_point start, end; | |
public: | |
Timer() { | |
start = std::chrono::high_resolution_clock::now(); | |
} | |
~Timer() { | |
end = std::chrono::high_resolution_clock::now(); | |
std::cout << "Execution took: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds\n"; | |
} | |
}; | |
// The timer measures time of executing a block of code, after the line it was constructed. | |
// For example, below code measures the time it took to execute the whole program, and prints | |
// it to the standard output. Time is measured in microseconds. | |
//int main() { | |
// Timer timer; | |
// /* code goes here */ | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment