0

I need to get the elapsed time from a while.

something.start() auto start = std::chrono::steadyt_clock::now(); while(something->is_valid()) { // getting frames auto end = std::chrono::steady_clock::now(); auto diff = end - start; // do something with the frames if(diff % 3 == 0) { /* do something with the something */ } } 

But it get the time in every ms i get the time and my if statement runs too much. I can not use std::this_thread::sleep_for() cause i need every frame to catch. How can i do it pararell?

1
  • 1
    Are you asking how to do duration_cast? Commented Dec 10, 2018 at 11:09

1 Answer 1

4

Since C++14 you could do diff >= 3s to see if the difference was equal or bigger than three seconds (see this duration literal reference).

Otherwise if you're stuck with C++11 then use diff >= std::chrono::seconds(3).

Note that this requires you to reset start each time the condition is true:

if (diff >= 3s) { start = end; // Do something with the something... } 

This is required because the difference could stay equal to 3 (and therefore diff % 3s == 0 being true) for up to a whole second, which means your "Do something..." part will execute many times falsely.

Sign up to request clarification or add additional context in comments.

1 Comment

@Aconcagua Yes, but that's kind of required anyway, since almost no duration is going to be an exact multiple of three, leading to diff % 3s being wrong sometimes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.