-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclock.cu
More file actions
73 lines (62 loc) · 1.27 KB
/
clock.cu
File metadata and controls
73 lines (62 loc) · 1.27 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
struct clock {
char* name;
long totalTime;
long startTime;
struct clock* next;
};
struct clock *clocks = NULL;
long time();
void startClock(char* name) {
struct clock *cp = clocks;
while (cp != NULL) {
if (strcmp(cp->name,name) == 0) {
clocks->startTime = time();
return;
}
cp = cp->next;
}
// if you are here, no match
cp = (struct clock*)malloc(sizeof(struct clock));
cp->name = (char*) malloc(strlen(name)+1);
strcpy(cp->name,name);
cp->totalTime = 0;
cp->startTime = time();
cp->next = clocks;
clocks = cp;
return;
}
void stopClock(char* name) {
struct clock *cp = clocks;
while (cp && strcmp(cp->name,name)) {
cp = cp->next;
}
if (cp && cp->startTime) {
cp->totalTime += (time() - cp->startTime);
cp->startTime = 0;
}
}
void dump() {
struct clock *cp = clocks;
while (cp) {
printf("%-20s %ld micros\n",cp->name, cp->totalTime);
cp = cp->next;
}
}
void printClock(char* name) {
struct clock *cp = clocks;
while (cp && strcmp(cp->name,name)) {
cp = cp->next;
}
if (cp) {
printf("%-20s %ld micros\n",cp->name,cp->totalTime);
}
}
long time() {
struct timeval tv;
gettimeofday(&tv,NULL);
return 1000000*(tv.tv_sec % (60*60*24*365)) + tv.tv_usec;
}