Last active
December 26, 2021 07:24
-
-
Save intergalacticspacehighway/2d5484300bf9bf60e0be9e1b047c0c53 to your computer and use it in GitHub Desktop.
Timer to measure performance in c++
This file contains hidden or 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> | |
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