Because start
isn't a duration, it's a time point. Also note that std::chrono::steady_clock
is not related to the wall clock, so getting its current value in itself doesn't really tell you that much, it's just a counter that steadily counts up.
Obtain time c++
-
03-07-2022 - |
Question
I am trying to convert a Windows c++ function to a portable one.
The objective of the function is to obtain a reference cpu time in seconds. The Windows function uses QueryPerformanceCounter
and QueryPerformanceFrequency
, which are not portable to mac, so I have tried to use std::chrono::high_resolution_clock
, but I do not understand how it works.
The function that I already have is the following:
double GetSeconds(void)
{
double sec;
LARGE_INTEGER Frequency, PerformanceCount;
QueryPerformanceFrequency( &Frequency );
QueryPerformanceCounter( &PerformanceCount );
sec = (double)PerformanceCount.QuadPart /(double)Frequency.QuadPart;
return(sec);
}
With high_resolution_clock
I have the following code working (it returns the time that printing "Hello World" takes):
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::cout << "Hello World\n";
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
float a = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
But I do not understand why the following line does not work (I just want to obtain the start time point):
float a = std::chrono::duration_cast<std::chrono::seconds>(start).count();
Thank you for your help
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow