debug_timer.c (1107B)
1 #include <time.h> 2 3 #include "debug_timer.h" 4 5 void debugTimerReset(debug_timer_t *time) { 6 time->running = 0; 7 time->paused = 0; 8 time->count = 0; 9 time->total = 0; 10 } 11 12 void debugTimerStart(debug_timer_t *time) { 13 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(time->snap_start)); 14 time->running = 1; 15 } 16 17 long debugTimerPause(debug_timer_t *time) { 18 if (!time->running) 19 return 0; 20 21 if (time->paused) { 22 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(time->snap_tmp)); 23 time->snap_start.tv_nsec += time->snap_tmp.tv_nsec - time->snap_end.tv_nsec; 24 time->paused = 0; 25 return time->snap_tmp.tv_nsec; 26 } else { 27 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(time->snap_end)); 28 time->paused = 1; 29 return time->snap_end.tv_nsec; 30 } 31 } 32 33 void debugTimerStop(debug_timer_t * time) { 34 if (time->running == 0) 35 return; 36 37 if (!time->paused) 38 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(time->snap_end)); 39 40 time->total = time->snap_end.tv_nsec - time->snap_start.tv_nsec; 41 time->count++; 42 time->running = 0; 43 time->paused = 0; 44 } 45 46 float debugTimerGetAverage(debug_timer_t *time) { 47 return time->total / (float)time->count; 48 }