-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTiming.h
More file actions
70 lines (65 loc) · 1.34 KB
/
Timing.h
File metadata and controls
70 lines (65 loc) · 1.34 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
#ifndef _TIMING_H_
#define _TIMING_H_
#include <vector>
#include <ostream>
#include <fstream>
#include <string>
class Timing {
public:
vector<long> ticks;
vector<string> labels;
int index;
clock_t curTime;
clock_t startTime;
void Start() {
index=0;
curTime=clock()/1000;
startTime=curTime;
}
int Elapsed() {
if (ticks.size() == 0) {
return 0;
}
else {
return clock()/1000 - startTime;
}
}
void Tick(string label) {
if (index +1 > ticks.size()) {
ticks.push_back(0);
}
long prev=ticks[index];
ticks[index] += clock()/1000 - curTime;
if (prev > ticks[index]) {
cerr << "Warning, tick has wrapped over long" << endl;
}
curTime=clock()/1000;
if (index + 1 > labels.size()) {
labels.push_back(label);
}
index+=1;
}
void Add(Timing &t) {
//
// Only add ticks the same size -- sometimes a thread
// will be created without adding ticks.
//
if (t.ticks.size() == ticks.size()) {
for (int i =0; i < t.ticks.size(); i++) {
ticks[i] += t.ticks[i];
}
}
}
void Summarize(const string &outFileName) {
long total=0;
ofstream outFile(outFileName.c_str());
for (int i=0; i<ticks.size(); i++) {
total+= ticks[i];
}
for (int i=0; i < ticks.size(); i++) {
outFile << labels[i] << "\t" << ticks[i] << "\t" << ((double)ticks[i])/total << endl;
}
outFile.close();
}
};
#endif