Created
February 3, 2018 21:38
-
-
Save pgiu/b7d76d13e5d549caa9ad4cf4642cc5ae to your computer and use it in GitHub Desktop.
Tests overhead of nanosleep in a Unix system
This file contains 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
// 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