Skip to content

Instantly share code, notes, and snippets.

@wojexe
Last active October 23, 2020 16:05
Show Gist options
  • Save wojexe/7366344ec685ddd6f2cda2bcfd9db28b to your computer and use it in GitHub Desktop.
Save wojexe/7366344ec685ddd6f2cda2bcfd9db28b to your computer and use it in GitHub Desktop.
C++ block execution timer
#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