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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:00:41+00:00 2026-06-03T18:00:41+00:00

When using the following example code, the tv_nsec value, I presume is looping round

  • 0

When using the following example code, the tv_nsec value, I presume is looping round because it’s only an long;

#include <iostream>
using namespace std;
#include <time.h>

int main(int argc, char *argv[]) {
  timespec time1, time2;
  timespec timens = {0};
  timens.tv_sec = 1;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  nanosleep(&timens, (struct timespec *)NULL);
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

  cout << time1.tv_nsec << " : " << time2.tv_nsec << endl;
  cout << (time2.tv_nsec-time1.tv_nsec) << endl;
  return 0;
}

$ ./microtime 
2754095 : 2766801
12706

The difference between the start and end time is 12706ns (12.7us), it didn’t take 12us to sleep for 1 second, I’m reasonably sure of that! So what has happened here, has tv_nsec looped round? Assuming it’s an singed long to give a smaller range of positive values (0 to 2147483647) instead of an unsigned long (0 to 4294967295), 2147483647 nanoseconds is still just over 2 full seconds!

If I change the code to the following I get a seemingly invalid output still;

timens.tv_sec = 0;
timens.tv_nsec = 100000;

$ ./microtime
2743552 : 2754327
10775

The sleep duration was set for 100,000ns but according to that output the program has slept for 10,000ns. I know that is a small amount of time but I’m trying to show here that whether it is a long time like 1 second or a short time like 100us, there seems to be no accuracy here, or anywhere in between these values. Am I doing something wrong, have I implemented this incorrectly somehow? Why can’t I get the actual duration of the event in between the calls to clock_gettime()?

Many thanks for your time.

Update

@Oli;
I can test your suggestion with the following code but it doesn’t seem to work:

int main(int argc, char *argv[]) {
  timespec time1, time2;
  timespec timens = {0};
  timens.tv_sec = 1;
  timens.tv_nsec = 0;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  nanosleep(&timens, (struct timespec *)NULL);
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

  cout <<time1.tv_sec<<"s:"<<time1.tv_nsec<<"ns : "<<
         time2.tv_sec<<"s:"<<time2.tv_nsec<<"ns"<<endl;
  cout <<(time2.tv_nsec-time1.tv_nsec)<<endl;
  return 0;
}

$ ./microtime
0s:2801478ns : 0s:2813732ns
12254
  • 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-03T18:00:45+00:00Added an answer on June 3, 2026 at 6:00 pm

    You also need to take the tv_sec field into account when calculating the time interval.

    UPDATE

    Also, CLOCK_PROCESS_CPUTIME_ID measures CPU time used for this process. If you yield with a nanosleep, this timer won’t increment. Try e.g. CLOCK_MONOTONIC instead.

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

Sidebar

Related Questions

I am using following example source code but I can't get long distance route
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Using the following example, I need to filter out the line containing 'ABC' only,
I have the following example code using MEF: public interface IFoo<T> {} public class
I'm using the google maps API with openstreetmap (see the following example code), I'd
I'm using the amazon-product-advertising-api gem and am following the example code to try and
In MongoDB-PHP I am using the following example code to push a new entry
I'm using the following example I found on the internet to dynamically build a
I'm trying to access $a using the following example: df<-data.frame(a=c(x,x,y,y),b=c(1,2,3,4)) > df a b
I am trying to create dynamic menus from the database using the following example

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.