Skip to content

Instantly share code, notes, and snippets.

@intergalacticspacehighway
Last active December 26, 2021 07:24
Show Gist options
  • Save intergalacticspacehighway/2d5484300bf9bf60e0be9e1b047c0c53 to your computer and use it in GitHub Desktop.
Save intergalacticspacehighway/2d5484300bf9bf60e0be9e1b047c0c53 to your computer and use it in GitHub Desktop.
Timer to measure performance in c++
#include <iostream>
#include <chrono>
class Timer
{
public:
Timer() {
startTimePoint = std::chrono::high_resolution_clock::now();
}
~Timer() {
auto endTimePoint = std::chrono::high_resolution_clock::now();
// change below from nano to milliseconds or microseconds if needed
auto start = std::chrono::time_point_cast<std::chrono::nanoseconds>(startTimePoint).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::nanoseconds>(endTimePoint).time_since_epoch().count();
auto duration = end - start;
std::cout << "duration :: " << duration << std::endl;
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> startTimePoint;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment