-1

I need to add some artificial latency to my code without using std::this_thread::sleep_for() to test some hardware. In fact, I need a function like:

addLatency(10m) // Do dummy computation for 10 milli second

I know that one possible solution is by doing a dummy calculation like:

long int j=0;
for (long int = 0; i < MAX_SIZE < i++)
    j++;

But, Is there any easier or logical way to do this? I searched a lot and could find out a lot of solutions to improve performance, but I could not find anything how to make the performance worst.

  • 1
    Why don't you want to use `sleep_for`? – interjay Mar 11 '21 at 10:18
  • 2
    Do you want to use 100% of processor power for those 10ms or to use 0%? For the latter, you need some sort of sleep or wait function. – Yksisarvinen Mar 11 '21 at 10:18
  • I cannot put the process to sleep, I must use the processing power. – Ehsan Sharifi Esfahani Mar 11 '21 at 10:21
  • Your loop will be optimized away completely if your compiler has optimizations turned on. – 463035818_is_not_an_ai Mar 11 '21 at 10:22
  • 2
    The obvious method would be `while (steady_clock::now() < target_time) {}`. – interjay Mar 11 '21 at 10:29
  • Not using sleep is a sin called "busy waiting". You must have good reasons... In addition, running a loop for a specific duration is challenging, unless you are running a real-time kernel. –  Mar 11 '21 at 10:41
  • 1. What platform and compiler is this for? 2. why is sleep not allowed: to warm up the CPU from low frequencies to the "cruise" speed for benchmarking purposes ? or because you do not have `sleep` ? or something else? 3. can you measure time? do you have timer counters? 4. the `j` should be `volatile` or even `volatile static` to avoid optimizing it out by compiler. How precise the time delay should be? – Spektre Mar 12 '21 at 06:35

1 Answers1

3

This loop on its own is not reliable to have any effect:

long int j=0;
for (long int = 0; i < MAX_SIZE < i++)
    j++;

Unless you use the value of j afterwards the compiler may remove the loop entirely. If you want to keep the CPU busy for some time you need to measure the elapsed time (copy-pasted & modified from here):

void addLatency(long ms) {
    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
    auto duration_ms = [&](){
        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
    };
    while( duration_ms() < ms) {}
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Mh, if the sleep function is not allowed, do we think that reading a clock is ? –  Mar 11 '21 at 10:46
  • @YvesDaoust polling the clock is not for free, while a sleeping thread isnt necessarily doing anything. OP is only concerned about keeping the cpu busy – 463035818_is_not_an_ai Mar 11 '21 at 10:48
  • This is not what I mean. Is it *allowed* ? –  Mar 11 '21 at 10:52
  • 1
    Why not just declare a global `volatile` variable and busy-loop incrementing that? – paddy Mar 11 '21 at 10:52
  • @YvesDaoust OP: "I cannot put the process to sleep, I must use the processing power." so I assumed yes. – 463035818_is_not_an_ai Mar 11 '21 at 10:54
  • @largest_prime_is_463035818 this also assumes the C++ implementation used has `std::chrono::steady_clock` ... OP clearly states `some HW` which implies MCU ... C++ compilers for MCUs are usually not compliant with C++ standards nor has full functionality ... which might be one of the reasons why `std::this_thread::sleep_for()` is not allowed in the first place – Spektre Mar 12 '21 at 06:31