Skip to content

Instantly share code, notes, and snippets.

@giuliano-oliveira
Last active Oct 4, 2018
Embed
What would you like to do?
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