1 | /* |
2 | * IXBench.cpp |
3 | * Author: Benjamin Sergeant |
4 | * Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved. |
5 | */ |
6 | |
7 | #include "IXBench.h" |
8 | |
9 | #include <iostream> |
10 | |
11 | namespace ix |
12 | { |
13 | Bench::Bench(const std::string& description) |
14 | : _description(description) |
15 | { |
16 | reset(); |
17 | } |
18 | |
19 | Bench::~Bench() |
20 | { |
21 | if (!_reported) |
22 | { |
23 | report(); |
24 | } |
25 | } |
26 | |
27 | void Bench::reset() |
28 | { |
29 | _start = std::chrono::high_resolution_clock::now(); |
30 | _reported = false; |
31 | } |
32 | |
33 | void Bench::report() |
34 | { |
35 | auto now = std::chrono::high_resolution_clock::now(); |
36 | auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - _start); |
37 | |
38 | _duration = microseconds.count(); |
39 | std::cerr << _description << " completed in " << _duration << " us" << std::endl; |
40 | |
41 | setReported(); |
42 | } |
43 | |
44 | void Bench::record() |
45 | { |
46 | auto now = std::chrono::high_resolution_clock::now(); |
47 | auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - _start); |
48 | |
49 | _duration = microseconds.count(); |
50 | } |
51 | |
52 | void Bench::setReported() |
53 | { |
54 | _reported = true; |
55 | } |
56 | |
57 | uint64_t Bench::getDuration() const |
58 | { |
59 | return _duration; |
60 | } |
61 | } // namespace ix |
62 | |