-
Notifications
You must be signed in to change notification settings - Fork 9
/
DurationTimer.h
55 lines (51 loc) · 1.62 KB
/
DurationTimer.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
#pragma once
#include "ofMain.h"
#include "OnlineStatistics.h"
/*
DurationTimer will keep track of how long something takes.
This is useful for timing functions to check performance.
Call start() and stop() to before and after the function
that you want to time.
*/
string humanDuration(float seconds, int precision) {
if(seconds > 60) {
int minutes = seconds / 60;
seconds = seconds - minutes * 60;
return ofToString(minutes) + "m" + ofToString(seconds, precision) + "s";
} else if(seconds > 1) {
return ofToString(seconds, precision) + "s";
} else if(seconds > 0.001) {
return ofToString(seconds / 0.001, precision) + "ms";
} else if(seconds > 1e-6) {
return ofToString(seconds / 1e-6, precision) + "us";
} else if(seconds > 1e-9) {
return ofToString(seconds / 1e-9, precision) + "ns";
}
}
class DurationTimer : public OnlineStatistics {
protected:
float lastTime;
public:
DurationTimer() :
lastTime(0) {
};
void start() {
lastTime = ofGetElapsedTimef();
}
float stop() {
float curTime = ofGetElapsedTimef();
float duration = curTime - lastTime;
update(duration);
return duration;
}
string getSummary(int precision=2) const {
stringstream msg;
msg << humanDuration(getMean(), precision);
if(getCount() > 1) {
msg << " +/-" << humanDuration(getStd(), precision) << " "
<< "(" << humanDuration(getMin(), precision) << " to " << humanDuration(getMax(), precision) << ") "
<< "x" << getCount();
}
return msg.str();
}
};