#include <time.h> int delay(int time) { int i,j; for(i =0;i<time;i++) for(j=0;j<10000;j++) ; } int main() { struct timespec start_1, end_1; unsigned long long diff; clock_gettime(CLOCK_REALTIME, &start_1); delay(10); // UUT unit under test clock_gettime(CLOCK_REALTIME, &end_1); diff = 1000000000 * (end_1.tv_sec - start_1.tv_sec) + (end_1.tv_nsec - start_1.tv_nsec); printf("the difference is %lld nano seconds\n",diff); return 0; }
reference : too muchcode