0

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

Ggsgn Hdjwngnf
  • 133
  • 2
  • 10
  • 1
    Does this answer your question? [Why does dividing two int not yield the right value when assigned to double?](https://stackoverflow.com/questions/7571326/why-does-dividing-two-int-not-yield-the-right-value-when-assigned-to-double) – Lukas-T Dec 19 '20 at 08:05
  • Does this answer your question? [Converting steady\_clock::time\_point to time\_t](https://stackoverflow.com/questions/18361638/converting-steady-clocktime-point-to-time-t) – Ghasem Ramezani Dec 19 '20 at 08:05
  • Ohh, so ```std::chrono::steady_clock::time_point``` is effectively an integer? – Ggsgn Hdjwngnf Dec 19 '20 at 08:07
  • @GgsgnHdjwngnf, `typedef chrono::time_point time_point;` – Ghasem Ramezani Dec 19 '20 at 08:11

1 Answers1

1

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;
}
Vroomy
  • 21
  • 2