Last active
October 4, 2018 01:01
-
-
Save giuliano-macedo/77791c2e120b593a71065b460d527a72 to your computer and use it in GitHub Desktop.
Not mt safe time.h nanoseconds timer function
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
#include<stdio.h> | |
#include <time.h> | |
struct timespec TIMER_START,TIMER_END; | |
long unsigned int timer(int f){ | |
if(f){ | |
clock_gettime(CLOCK_MONOTONIC_RAW, &TIMER_START); | |
return 0; | |
} | |
clock_gettime(CLOCK_MONOTONIC_RAW, &TIMER_END); | |
return (TIMER_END.tv_sec - TIMER_START.tv_sec) * 1e9 + (TIMER_END.tv_nsec - TIMER_START.tv_nsec); | |
} | |
int main(){ | |
timer(1);//start | |
for(int i=0;i<10000000;i++); | |
printf("operation took %lu nanoseconds\n",timer(0)); //end | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment