-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
38 lines (29 loc) · 903 Bytes
/
timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef _TIMER_H_
#define _TIMER_H_
#include <stdio.h>
#include <sys/time.h>
enum PrintColor { NONE, GREEN, DGREEN, CYAN };
typedef struct {
struct timeval startTime;
struct timeval endTime;
} Timer;
static void startTime(Timer* timer) {
gettimeofday(&(timer->startTime), NULL);
}
static void stopTime(Timer* timer) {
gettimeofday(&(timer->endTime), NULL);
}
static void printElapsedTime(Timer timer, const char* s, enum PrintColor color = NONE) {
float t = ((float) ((timer.endTime.tv_sec - timer.startTime.tv_sec) \
+ (timer.endTime.tv_usec - timer.startTime.tv_usec)/1.0e6));
switch(color) {
case GREEN: printf("\033[1;32m"); break;
case DGREEN: printf("\033[0;32m"); break;
case CYAN : printf("\033[1;36m"); break;
}
printf("%s: %f ms\n", s, t*1e3);
if(color != NONE) {
printf("\033[0m");
}
}
#endif