-4

I am slowing down the execution of the if statement in this way. But I do not like him.

for (size_t i = 0;;) {
   i = (i + 1) % 1000000;
   if (i == 10) {
      cout << " TEST " << "\n" << endl;
   }
}

I would like to make the code on time. Until one minute passes, the if statement should always be passed by.

How can this be realized?

for (;;) { 
  /* do something */ 
  if (one_minute_elapsed_since_first_iteration) { 
  /* do something else */ 
  } 
}

And one more question will it work faster than the first option?

i friend
  • 11
  • 4
  • 3
    [`std::time`](https://en.cppreference.com/w/cpp/chrono/c/time). – HolyBlackCat Jan 16 '20 at 15:01
  • Hey, welcome to Stack Overflow! You're gonna need to store the time at the start of the loop *outside of the loop*, then check if one minute has passed in the if statement. This should help: https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c – grooveplex Jan 16 '20 at 15:02

1 Answers1

3

If you just want to add a delay in your if, then std::this_thread::sleep_for may be what you want. It will put your code to sleep for some specified duration and then continue running when the time has expired.

For example, to sleep for one minute you could do

#include <chrono>
#include <thread>
...
using namespace std::chrono_literals;
...
std::this_thread::sleep_for(1m);

To answer the updated question :

"How can this be realized?

for (;;) { /* do something */ if (one_minute_elapsed_since_first_iteration) { /* do something else */ } }"

You need to get a timestamp when the loop starts, then check if more than the required time has passed. For example:

#include <chrono>
using namespace std::chrono_literals;
auto start = std::chrono::steady_clock::now();
for (;;) {
    /* Do something */
    auto now = std::chrono::steady_clock::now();
    if (std::chrono::duration_cast<std::chrono::seconds>(now - start) >= 60s) {
        /* Do something else */
        /* If we should again wait 1 min. Reset start time */
        start = std::chrono::steady_clock::now();
    }
}
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • I think he wants to be able to exit the for loop after a timeout, or something like that (not my DV) – Cid Jan 16 '20 at 15:04
  • 2
    @Cid might be, but thats not what OPs code is telling. The answer is fine for the code in the question. – 463035818_is_not_an_ai Jan 16 '20 at 15:06
  • @Cid That's not what OP is asking though. I'm just trying to answer the question *as asked*. – Jesper Juhl Jan 16 '20 at 15:07
  • the cycle should not stop due to `if` – i friend Jan 16 '20 at 15:09
  • @JesperJuhl I thought OP asked about how to sleep for x time at first, but I am not really sure that's what he meant, question could be interpreted by *"how to do :*" `while (true) { /* do something */ if (one_minute_elapsed_since_first_iteration) { /* do something else */ } }` – Cid Jan 16 '20 at 15:10
  • @ifriend would you please rephrase your question and add clarifications ? – Cid Jan 16 '20 at 15:12
  • 1
    @ifriend if you want to pause the execution for 1 minute, this answer is perfect for your needs, else, you need to rephrase – Cid Jan 16 '20 at 15:13
  • @ifriend there is no substantial difference between `while(true)` and `for(;;)`. It is not clear how this does not answer your question – 463035818_is_not_an_ai Jan 16 '20 at 15:31
  • only the last example misses to reset the start time ;) – 463035818_is_not_an_ai Jan 16 '20 at 15:32
  • 1
    @formerlyknownas_463035818 it's not clear if that's wanted or not. If it is, it's trivial for OP to do. In any case, added that to the answer as well. – Jesper Juhl Jan 16 '20 at 15:34
  • oh, right also OPs code is executing the conditional part only once, sorry for adding more confusion ;) – 463035818_is_not_an_ai Jan 16 '20 at 15:36
  • a little bit not what I wanted, it was necessary that after each execution he would count 1 minute again – i friend Jan 16 '20 at 15:36
  • @ifriend My latest update should take care of your "count 1 minute again" need. I hope it now answers your question. – Jesper Juhl Jan 16 '20 at 15:41
  • Thank you, now this is what you need. Last question, what will work faster? 1 option that I showed above or this – i friend Jan 16 '20 at 15:42
  • @ifriend What do you mean "which will be faster"? The first thing you showed with `i = (i + 1) % 1000000;` is irrelevant since there's no telling how long it will take. That depends on what machine you are running on and the load of that machine - completely unreliable. My example on the other hand will do the "other stuff" after exactly 60 seconds have passed or as soon as possible thereafter, regardless of what machine you run it on. So it's not a matter of "what's faster", it's a matter of ""one method works and the other does not". – Jesper Juhl Jan 16 '20 at 15:47
  • Yes, I also thought so that the car can be slower, therefore, it created a question. I just wanted to know the code itself will work faster, or rather iterate, because there time checking can take more iterations in a loop. – i friend Jan 16 '20 at 15:54