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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:42:47+00:00 2026-05-27T08:42:47+00:00

I need to time my C program inside the program itself (as I am

  • 0

I need to time my C program inside the program itself (as I am timing multiple subsections of the code and outputting their time). It must be done with (at least) millisecond accuracy. My understanding is that “time.h” functions only work to the second. Is there another way to do this?

  • 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-05-27T08:42:48+00:00Added an answer on May 27, 2026 at 8:42 am

    You can use the function clock() from <time.h>:

    The clock() function returns an approximation of processor time used by the program.

    You divide the return value by the macro CLOCKS_PER_SEC to get a time amount in seconds, and if you wanted the time in milliseconds you could multiply the value by 1000, ie:

    double get_time_as_ms(void) {
       return ((double)(clock() * 1000) / CLOCKS_PER_SEC);
    }
    

    Or more accurately, have a start and end clock_t and then calculate the difference:

    double duration_as_ms(clock_t start, clock_t end) {
        return ((double)(end - start) * 1000) / CLOCKS_PER_SEC;
    
    }
    
    clock_t start = clock(); /* start of program */
    /* ... */
    clock_t end = clock(); /* end of program */
    /* ... */
    printf("Duration: %fms\m", duration_as_ms(start, end));
    

    EDIT: Just thought I’d add, a common value for CLOCKS_PER_SEC is 1000000, meaning that the value returned by clock() would be a one-hundred-thousandth of a second. Therefore it’s accurate to one one-thousandth of a millisecond (a microsecond).

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

Sidebar

Related Questions

I need to start a program every time the user logs in, but I
I need a very accurate way to time parts of my program. I could
Every time I need to do something N times inside an algorithm using C#
I am implementing a system where I need real-time updates. I have been looking
Every time I need to work with date and/or timstamps in Java I always
Often time I need to add a control to a dialog after the dialog
I need to implement time calculation for repository methods in my asp .net mvc
From time to time I need to run a full build of the entire
How can I get current time (I need hours, minutes, seconds) crossplatform in c++?
I'm working with jQuery for the first time and need some help. I have

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.