-
Notifications
You must be signed in to change notification settings - Fork 178
/
perftest.h
80 lines (67 loc) · 1.6 KB
/
perftest.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
78
79
80
#pragma once
#include <chrono>
namespace perftest
{
class Timer
{
public:
Timer()
: start()
, stop()
, poststop()
, lastspan()
, spans()
{
}
void Start()
{
// Timestamp
start = std::chrono::high_resolution_clock::now();
}
void Stop()
{
// Timestamps
stop = std::chrono::high_resolution_clock::now();
poststop = std::chrono::high_resolution_clock::now();
// Calculate duration
std::chrono::duration<double> span =
std::chrono::duration_cast<std::chrono::duration<double>>(stop - start - (poststop - stop));
// Store
lastspan = span.count();
spans.push_back(lastspan);
}
double GetLastDurationSec()
{
return lastspan;
}
double GetLastDurationUs()
{
return GetLastDurationSec() * 1000000.0;
}
double GetMedianDurationSec()
{
double median = 0;
if (spans.size() > 0)
{
sort(spans.begin(), spans.end());
median = spans.at(spans.size() / 2);
}
return median;
}
double GetMedianDurationUs()
{
return GetMedianDurationSec() * 1000000.0;
}
void ReportMedian()
{
std::cout << "Test median duration Elapsed ";
std::cout << static_cast<long long>(round(GetMedianDurationUs())) << " us\n";
}
private:
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point stop;
std::chrono::high_resolution_clock::time_point poststop;
double lastspan;
std::vector<double> spans;
};
}