Reputation: 171
How can I convert std::chrono::steady_clock::time_point
to a double after calculating the change in time.
int main()
{
std::chrono::steady_clock::time_point start, finish;
start = std::chrono::steady_clock::now();
finish = start;
while (true){
long double elapsedTime = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() / 1000000000;
std::cout << "Time elapsed (in seconds), " << elapsedTime << std::endl;
}
return 0;
}
Edit: it works, but it only returns 0 or 1, what im looking for is like 1.13715263724
or something
Upvotes: 0
Views: 928
Reputation: 21
First, you should update start at the begin of the loop, and finish at the end. And to get a floating value, 1000000000 have to be replaced by 1000000000.0 :
while (1) {
start = std::chrono::steady_clock::now();
long double elapsedTime = std::chrono::duration_cast<std::chrono::nanoseconds>(start - finish).count() / 1000000000.0;
std::cout << "Time elapsed (in seconds), " << elapsedTime << std::endl;
finish = start;
}
Upvotes: 1