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

  • Home
  • SEARCH
  • 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 6821221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:31:10+00:00 2026-05-26T21:31:10+00:00

Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer

  • 0

Possible Duplicate:
C++ Timer function to provide time in nano seconds
High resolution timer with C++ and Linux?

I would like to time a few C functions, and I’m trying to come up with a systematic way of doing so. So far, what I have is the simplest form of timing:

#include <sys/time.h>
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
<my function>
gettimeofday(&end_time, NULL);
total_usecs = (end_time.tv_sec-start_time.tv_sec) * 1000000 + 
    (end_time.tv_usec-start_time.tv_usec);
printf ("parfib,%ld,%d\n",(long)res,total_usecs); 

I have also found

timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
     <my function>
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;

But even with std=gnu99 I have not been able to get this to work (I copied the above code from a website, but I did translated into C before compiling :). Regardless, I’m wondering if any of you know of docs or blogs or something that discusses timing functions like I’m trying to do. Note, I’m not after profilers such as gprof or the like, I need this profiler to be hand-made.

  • 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-26T21:31:10+00:00Added an answer on May 26, 2026 at 9:31 pm

    To compile the second example you will need:

    gcc -Wall -Wextra -D_POSIX_C_SOURCE=199309L timer.c -lrt
    

    _POSIX_C_SOURCE specifies which POSIX version you’re targeting – it needs to be defined before any system header files get included, so doing it via -D is safest. clock_gettime also depends upon linking to librt (-lrt).

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

Sidebar

Related Questions

Possible Duplicate: fast elapsed time on linux How can I measure how long a
Possible Duplicate: C# Execute function at specific time I want to run certain function
Possible Duplicate: how to hide a div after some time period? i need to
Possible Duplicate: How can I specify the latest time of day with DateTime I
Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: Chaining a function in JavaScript? I asked a similar question , but
Possible Duplicate: C++ passing variables in from one Function to the Next. The Program
Possible Duplicate: Javascript: var functionName = function() {} vs function functionName() {} Way 1:
Possible Duplicate: Get the Files inside a directory Is there a function that can
Possible Duplicate: How does foreach work when looping through function results? Title has the

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.