Skip to content

Instantly share code, notes, and snippets.

@yudhastyawan
Created January 27, 2020 05:32
Show Gist options
  • Save yudhastyawan/05b0591edafc7d591e7ca090863272c4 to your computer and use it in GitHub Desktop.
Save yudhastyawan/05b0591edafc7d591e7ca090863272c4 to your computer and use it in GitHub Desktop.
Check your C++ code performance (speed time)
#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;
};
@yudhastyawan
Copy link
Author

// Example of code
`int main()
{
{
Timer timer;
int value = 0;
for(unsigned i = 0; i < 100000; ++i) value += i;
}

return 0;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment