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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:28:15+00:00 2026-06-08T18:28:15+00:00

I wanted to check the reliability of clock_gettime , using the deprecated gettimeofday as

  • 0

I wanted to check the reliability of clock_gettime, using the deprecated gettimeofday as reference, but am getting strange results sometimes:

#include <stdio.h>
#include <sys/time.h>
#include <iostream>

void clock_gettime_test()
{
    struct timespec tp;
    clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
    long a = tp.tv_nsec;
    usleep(250000);
    clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
    long b = tp.tv_nsec;
    printf("clock_gettime (%ld - %ld): %lf msec\n", b, a, (b - a)/1000000.0);
}

void gettimeofday_test()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    long a = tv.tv_usec;
    usleep(250000);
    gettimeofday(&tv, NULL);
    long b = tv.tv_usec;
    printf("gettimeofday (%ld - %ld): %lf msec\n", b, a, (b - a)/1000.0);
}

int main()
{
    clock_gettime_test();
    gettimeofday_test();
    return 0;
}

Building and running, I sometimes get the right results:

$ g++ -Wall play.cpp -lrt && ./a.out
clock_gettime (392441961 - 142299879): 250.142082 msec
gettimeofday (592906 - 342644): 250.262000 msec

But sometimes, me not so lucky:

clock_gettime (155321165 - 905000848): -749.679683 msec
gettimeofday (352232 - 101938): 250.294000 msec

Or even:

clock_gettime (947857371 - 697373625): 250.483746 msec
gettimeofday (225208 - 974908): -749.700000 msec

I have tried this both on an i686 and an amd64, and get similar results. I also tried nanosleep, with the same sad results.

  • 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-08T18:28:17+00:00Added an answer on June 8, 2026 at 6:28 pm

    You’re only comparing the tv_nsec member of struct timespec, while you should also be comparing the tv_sec member :

    double msec = ((tpb.tv_sec - tpa.tv_sec) * 1000.0)
                + ((tpb.tv_nsec - tpa.tv_nsec) / 1000000.0);
    

    Similarly, you should be comparing both the tv_sec and tv_usec members of struct timeval to get the elapsed time :

    double msec = ((tvb.tv_sec - tva.tv_sec) * 1000.0)
                + ((tvb.tv_usec - tva.tv_usec) / 1000.0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wanted to write some recursion but can't check if the child is in
I assume not, but I just wanted to check - is there any way
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
I am using Selenium IDE to record some scenarios and wanted to check if
This works -- it does compile -- but I just wanted to check if
Not sure if this is possible, but I wanted to check it out. I
I am using openGL and glew to check the extensions. Say I wanted to
I'm not sure if this can be done. But I just wanted to check
This might be a bit of a stretch but just wanted to check before
I wanted to check my JavaScript with JSLint. I am also using jQuery and

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.