c++loopsfor-loopperiodicityperiodic-processing

Call an expensive function periodically inside a loop


I call a computationally expensive function inside a loop:

for( int j = 0; j < Max; ++j ) {

    // ...

    processQueuedEvents(); // Computationally expensive call

    // ...

}

However, I don't need to run the expensive function on every single loop iteration, so I want to call it periodically:

for( int j = 0; j < Max; ++j ) {

    // ...

    if ( /* The condition I'm talking about */ ) 
        processQueuedEvents(); // Computationally expensive call

    // ...

}

At this point, I need to develop a proper condition for my periodic call. The condition should correlate to Max, I mean, if Max is larger, the expensive call is less-frequent and if Max is smaller the expensive call is more-frequent.

Does anybody have any suggestions or hints? For some reason, I have a hard time coming up with a proper condition.


Solution

  • You didn't provide enough details about the increment function you want to use. If you are considering a liner one you can reason with factors of 10 in the following manner:

    and so on.