-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
78 lines (64 loc) · 1.85 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2020, American University of Beirut
// See LICENSE.txt for copyright license
#ifndef _TIMER_H_
#define _TIMER_H_
#include <stdio.h>
#include <sys/time.h>
enum PrintColor { NONE, GREEN, CYAN };
struct Timer {
unsigned int isActive;
struct timeval startTime;
struct timeval endTime;
};
static Timer initTimer(unsigned int isActive) {
Timer timer;
timer.isActive = isActive;
return timer;
}
static void startTimer(Timer* timer) {
if(timer->isActive) {
gettimeofday(&(timer->startTime), NULL);
}
}
static void printAndStart(Timer* timer, const char* s) {
if(timer->isActive) {
printf("%s", s);
startTimer(timer);
}
}
static void stopTimer(Timer* timer) {
if(timer->isActive) {
gettimeofday(&(timer->endTime), NULL);
}
}
static void printElapsedTime(Timer* timer, const char* s = NULL, enum PrintColor color = NONE) {
if(timer->isActive) {
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[0;32m"); break;
case CYAN : printf("\033[0;36m"); break;
}
if(s == NULL) {
printf("%f ms\n", t*1e3);
} else {
printf("%s: %f ms\n", s, t*1e3);
}
if(color != NONE) {
printf("\033[0m");
}
}
}
static void stopAndPrintElapsed(Timer* timer, const char* s = NULL, enum PrintColor color = NONE) {
if(timer->isActive) {
stopTimer(timer);
printElapsedTime(timer, s, color);
}
}
static void syncStopAndPrintElapsed(Timer* timer, const char* s = NULL, enum PrintColor color = NONE) {
if(timer->isActive) {
cudaDeviceSynchronize();
stopAndPrintElapsed(timer, s, color);
}
}
#endif