Performing measures within the execution of a c++ code every t milliseconds

Posted by user506901 on Stack Overflow See other posts from Stack Overflow or by user506901
Published on 2012-06-02T14:30:50Z Indexed on 2012/06/02 16:41 UTC
Read the original article Hit count: 196

Filed under:
|

Given a while loop and the function ordering as follows:

int k=0;
int total=100;
while(k<total){
   doSomething();
   if(approx. t milliseconds elapsed) { measure(); }
   ++k;
}

I want to perform 'measure' every t-th milliseconds. However, since 'doSomething' can be close to the t-th millisecond from the last execution, it is acceptable to perform the measure after approximately t milliseconds elapsed from the last measure. My question is: how could this be achieved?

One solution would be to set timer to zero, and measure it after every 'doSomething'. When it is withing the acceptable range, I perform measures, and reset. However, I'm not which c++ function I should use for such a task. As I can see, there are certain functions, but the debate on which one is the most appropriate is outside of my understanding. Note that some of the functions actually take into account the time taken by some other processes, but I want my timer to only measure the time of the execution of my c++ code (I hope that is clear). Another thing is the resolution of the measurements, as pointed out below. Suppose the medium option of those suggested.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c