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 1059569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:11:37+00:00 2026-05-16T18:11:37+00:00

I have a particular function (well, set of functions) that I want to start

  • 0

I have a particular function (well, set of functions) that I want to start every 400ms. I’m not much of a C programmer, and so anything outside of the standard libraries is a mystery to me, as well as quite a bit within them.

My first thought is to use nanosleep to pause execution for 400ms in some sort of loop, but this of course doesn’t take into account the execution time of the code I will be running. If I could measure it, and if it seemed fairly certain that it ran for the same approximate duration after 10 or 20 tests, I could then nanosleep() for the difference. This wouldn’t be perfect, of course… but it might be close enough for a first try.

How do I measure the execution time of a C function? Or is there a better way to do this altogether, and what keywords do I need to be googling for?

  • 1 1 Answer
  • 1 View
  • 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-05-16T18:11:37+00:00Added an answer on May 16, 2026 at 6:11 pm

    You should be able to use settimer

    int setitimer(int which, const struct itimerval *value,
                  struct itimerval *ovalue);
    

    Just put the code that you want to execute every 400ms inside the SIGALRM handler. This way you don’t need to account for the time that your code takes to run, which could potentially vary. I’m not sure what happens if the signal handler doesn’t return before the next signal is generated.

    An outline of what some of the code might look like is shown below.

    void periodic_fuc(int signal_num)
    {
      ...
      signam(SIGALRM, periodic_func);
    }
    
    int main(...)
    {
      struct itimerval timerval = {0};
    
      signal(SIGALRM, periodic_func);      
      ...
      timerval.it_interval.tv_usec = 400000;
      timerval.it_value.tv_usec = 400000; // Wait 400ms for first trigger
      settimer(ITIMER_REAL, &timerval, NULL);
    
      while (!exit)
        sleep(1);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one particular FLA that is crashing every time I try to compile
I have a set of functions that are templated both by an integer type
I have a particular module in Symfony 1.4.13 that is set to be secured
I want to show/hide items that have a particular class (the one of the
I have a prototype representing a particual IFrame. That prototype have a function called
I have start date and end date. I also have particular day i.e. wednesday.
I have a particular file in a git source code repository that contains production
I have a particular application that needs to calculate something very specific, and while
I have a particular web application that like most others, has to account for
I have a particular method that is occasionally crashing with an ArgumentException: Destination array

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.