Created
January 27, 2020 05:32
-
-
Save yudhastyawan/05b0591edafc7d591e7ca090863272c4 to your computer and use it in GitHub Desktop.
Check your C++ code performance (speed time)
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 <iostream> | |
#include <chrono> | |
class Timer | |
{ | |
public: | |
Timer() | |
{ | |
m_StartTimePoint = std::chrono::high_resolution_clock::now(); | |
} | |
~Timer() | |
{ | |
Stop(); | |
} | |
void Stop() | |
{ | |
auto endTimePoint = std::chrono::high_resolution_clock::now(); | |
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count(); | |
auto stop = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count(); | |
auto duration = stop - start; | |
double ms = duration * 0.001; | |
std::cout << duration << "us (" << ms << "ms)\n"; | |
} | |
private: | |
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Example of code
`int main()
{
{
Timer timer;
int value = 0;
for(unsigned i = 0; i < 100000; ++i) value += i;
}
}`