Skip to content

Instantly share code, notes, and snippets.

@blgnksy
Last active December 6, 2020 17:48
Show Gist options
  • Save blgnksy/b38dedcb91553e60a408ea6a3d19a718 to your computer and use it in GitHub Desktop.
Save blgnksy/b38dedcb91553e60a408ea6a3d19a718 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <iostream>
#include <thread>
class Timer {
private:
std::chrono::time_point<std::chrono::high_resolution_clock> mStart;
public:
Timer() { mStart = std::chrono::high_resolution_clock::now(); }
~Timer() { stop(); }
void stop() {
auto end = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(mStart)
.time_since_epoch()
.count();
auto stop = std::chrono::time_point_cast<std::chrono::microseconds>(end)
.time_since_epoch()
.count();
auto duration = stop - start;
std::cout << duration * 0.001 << " ms\n";
}
};
using namespace std::literals::chrono_literals;
int main()
{
{
auto t = Timer();
std::this_thread::sleep_for(1s);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment