-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimer.h
More file actions
53 lines (44 loc) · 968 Bytes
/
timer.h
File metadata and controls
53 lines (44 loc) · 968 Bytes
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
#ifndef TIMER_H
#define TIMER_H
#ifdef __linux__
#include <sys/time.h>
#else
#include <time.h>
#endif
#include <stdio.h>
double getTime(){
#ifdef __linux__
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec * 1e-6;
#else
return 1.0 * clock() / CLOCKS_PER_SEC;
#endif
}
#define tic tic_f(__LINE__);
#define toc toc_f(__LINE__);
#define MAX_LINE 10000
double _timer[MAX_LINE] = {0};
int _fromLine[MAX_LINE] = {0};
int _lastLineNum = -1;
double _lastRdtsc = 0;
inline void tic_f(int line){
_lastLineNum = line;
_lastRdtsc = getTime();
}
inline void toc_f(int line){
double t = getTime();
_fromLine[line] = _lastLineNum;
_timer[line] += t - _lastRdtsc;
_lastLineNum = line;
_lastRdtsc = t;
}
inline void tictoc(FILE *out){
int i;
for(i = 0; i < MAX_LINE; i++){
if(_timer[i] != 0)
fprintf(out, "line %d - %d: %.3f\n", _fromLine[i], i, _timer[i]);
}
fflush(out);
}
#endif