Skip to content

Instantly share code, notes, and snippets.

@giuliano-macedo
Last active October 4, 2018 01:01
Show Gist options
  • Save giuliano-macedo/77791c2e120b593a71065b460d527a72 to your computer and use it in GitHub Desktop.
Save giuliano-macedo/77791c2e120b593a71065b460d527a72 to your computer and use it in GitHub Desktop.
Not mt safe time.h nanoseconds timer function
#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