Created
November 12, 2010 01:51
-
-
Save oraccha/673601 to your computer and use it in GitHub Desktop.
nanosleep(2) accuracy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* source code from http://d.hatena.ne.jp/naoya/20080122 | |
* | |
* result on Debian/sid kernel 2.6.32-5-amd64 | |
* (gettimeofday(2) overhead: 41us) | |
* | |
* interval 1ms | |
* 1289525521:664885 | |
* 1289525521:665938 | |
* 1289525521:666991 | |
* 1289525521:668043 | |
* 1289525521:669096 | |
* 1289525521:670148 | |
* 1289525521:671203 | |
* 1289525521:672256 | |
* 1289525521:673308 | |
* 1289525521:674360 | |
* | |
* interval 100us | |
* 1289526832:275236 | |
* 1289526832:275389 | |
* 1289526832:275541 | |
* 1289526832:275694 | |
* 1289526832:275846 | |
* 1289526832:275999 | |
* 1289526832:276151 | |
* 1289526832:276304 | |
* 1289526832:276456 | |
* 1289526832:276608 | |
* | |
* interval 10us | |
* 1289527778:632163 | |
* 1289527778:632226 | |
* 1289527778:632288 | |
* 1289527778:632352 | |
* 1289527778:632414 | |
* 1289527778:632476 | |
* 1289527778:632538 | |
* 1289527778:632602 | |
* 1289527778:632664 | |
* 1289527778:632726 | |
* | |
* interval 1us | |
* 1289525667:387121 | |
* 1289525667:387175 | |
* 1289525667:387229 | |
* 1289525667:387282 | |
* 1289525667:387335 | |
* 1289525667:387389 | |
* 1289525667:387442 | |
* 1289525667:387495 | |
* 1289525667:387548 | |
* 1289525667:387588 | |
*/ | |
#define _POSIX_C_SOURCE 200112L | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#include <unistd.h> | |
enum { | |
LOOP = 10, | |
}; | |
int main (int argc, char **argv) | |
{ | |
struct timeval tv[LOOP]; | |
struct timespec req; | |
req.tv_sec = 0; | |
req.tv_nsec = 1000000; // 1ms | |
for (int i = 0; i < LOOP; i++) { | |
nanosleep(&req, NULL); | |
gettimeofday(tv + i, NULL); | |
} | |
for (int i = 0; i < LOOP; i++) | |
printf("%ld:%06ld\n", (long)tv[i].tv_sec, (long)tv[i].tv_usec); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment