I was working with the Chrono library for time measurement. I find out the following code and I know how to use it.
class Timer {
private:
std::chrono::time_point<std::chrono::high_resolution_clock> pr_StartTime;
std::chrono::time_point<std::chrono::high_resolution_clock> pr_EndTime;
public:
Timer()
{
Start();
}
~Timer()
{
Finish();
}
void Start()
{
pr_StartTime = std::chrono::high_resolution_clock::now();
}
void Finish()
{
pr_EndTime = std::chrono::high_resolution_clock::now();
auto StartTimeMs = std::chrono::time_point_cast<std::chrono::microseconds>(pr_StartTime).time_since_epoch().count();
auto EndTimeMs = std::chrono::time_point_cast<std::chrono::microseconds>(pr_EndTime).time_since_epoch().count();
auto Duration = EndTimeMs - StartTimeMs;
std::cout << "Duration " << Duration << " microseconds." << std::endl;
}
};
But I didn't realize why the developer used time_since_epoch().count() in casting step. Why we should use time_since_epoch() and count()?