Skip to content

Instantly share code, notes, and snippets.

@pgiu
Created February 3, 2018 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgiu/b7d76d13e5d549caa9ad4cf4642cc5ae to your computer and use it in GitHub Desktop.
Save pgiu/b7d76d13e5d549caa9ad4cf4642cc5ae to your computer and use it in GitHub Desktop.
Tests overhead of nanosleep in a Unix system
// Tests overhead of nanosleep in a Unix system
// Compile it like this:
// gcc -o test_nanosleep test_nanosleep -lrt
// Tested in Solaris
#include <stdio.h>
#include <time.h>
int main()
{
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 1e6;
struct timeval start_time;
struct timeval end_time;
gettimeofday(&start_time, NULL);
if(nanosleep(&tim , &tim2) < 0 )
{
printf("Nano sleep system call failed \n");
return -1;
}
gettimeofday(&end_time, NULL);
// Time elapsed in microseconds
int elapsed = (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec);
printf("Time elapsed %.3fms\n", elapsed/1000.0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment