Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:38:08+00:00 2026-06-04T21:38:08+00:00

Given a while loop and the function ordering as follows: int k=0; int total=100;

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:38:10+00:00Added an answer on June 4, 2026 at 9:38 pm

    High resolution timing is platform specific, and you have not specified in the question. The standard library clock() function returns a count that increments at CLOCKS_PER_SEC per second. On some platforms this may be fast enough to give you the resolution you need but you should check your system’s tick rate since it is implementation defined. However if you find it is high enough then:

    #define SAMPLE_PERIOD_MS 100
    #define SAMPLE_PERIOD_TICKS ((CLOCKS_PER_SEC * SAMPLE_PERIOD_MS) / 1000)
    
    int k=0;
    int total=100;
    clock_t measure_time = clock() + SAMPLE_PERIOD_TICKS  ;
    while(k<total)
    {
       doSomething();
       if( clock() - measure_time > 0 )
       { 
            measure(); 
            measure_time += SAMPLE_PERIOD_TICKS ;
            ++k;
       }
    }
    

    You might replace clock() with some other high-resolution clock source if necessary.

    However note a couple of issues. This method is a “busy-loop”; unless either doSomething() or measure() yield the CPU, the process will take all the cpu cycles it can. If this is the only code running on a target, that may not matter. On the other hand is this is running on a general purpose OS such as Windows or Linux which are not real-time, the process may be pre-empted by other processes, and this may affect the accuracy of the sampling periodicity. If you need accurate timing use of an RTOS and performing doSomething() and measure() in separate threads would be better. Even in a GPOS that would be better. For example a general pattern (using a made-up API in teh absence of any specification) would be:

    int main()
    {
        StartThread( measure_thread, HIGH_PRIORITY ) ;
        for(;;)
        {
            doSomething() ;
        }
    }
    
    void measure_thread()
    {
        for(;;)
        {
            measure() ;
            sleep( SAMPLE_PERIOD_MS ) ;
        }
    }
    

    The code for measure_thread() is only accurate if measure() takes a negligible time to run. If it takes significant time you may need to account for that. If it is non-deterministic, you may even have to measure its execution time in order to subtract it the sleep period.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While looking for ways to find the size of a file given a FILE*
How can I rotate an Bitmap a given number of degrees while maintaining the
I want to give zoom effect to the iPhone camera image while capturing the
Reading this question got me thinking: For a given function f , how can
Using PHP , I would like to make a while loop that reads a
Given a function zipdistance(zipfrom,zipto) which calculates the distance (in miles) between two zip codes
An empty $movies array given to the for loop will yield a warning. Checking
Edit: With the answer given I made this function function grabclosestcolor($r, $g, $b){ $colors
I'm new to programing and was given a task of making a function that
do{ <div class=fine id=<?php echo $i; ?>><?php echo $tb_dt_01 ?></div> }while.......... $($('.fine').attr('id')).mouseover(function() { alert($('.fine').attr('id'));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.