Graph Framework
Loading...
Searching...
No Matches
timing.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
4//------------------------------------------------------------------------------
5
6#ifndef timing_h
7#define timing_h
8
9#include <chrono>
10#include <mutex>
11#include <map>
12
14namespace timing {
15//------------------------------------------------------------------------------
17//------------------------------------------------------------------------------
19 private:
21 const std::string label;
23 const std::chrono::high_resolution_clock::time_point start;
25 std::chrono::high_resolution_clock::time_point end;
26
27 public:
28//------------------------------------------------------------------------------
32//------------------------------------------------------------------------------
33 measure_diagnostic(const std::string message = "") :
34 label(message), start(std::chrono::high_resolution_clock::now()) {}
35
36//------------------------------------------------------------------------------
38//------------------------------------------------------------------------------
39 void print() const {
40 const auto end = std::chrono::high_resolution_clock::now();
41 const auto total_time = end - start;
42 const std::chrono::nanoseconds total_time_ns =
43 std::chrono::duration_cast<std::chrono::nanoseconds> (total_time);
44
45 std::cout << std::endl << " " << label << " : ";
46
47 if (total_time_ns.count() < 1000) {
48 std::cout << total_time_ns.count() << " ns" << std::endl;
49 } else if (total_time_ns.count() < 1000000) {
50 std::cout << total_time_ns.count()/1000.0 << " μs" << std::endl;
51 } else if (total_time_ns.count() < 1000000000) {
52 std::cout << total_time_ns.count()/1000000.0 << " ms" << std::endl;
53 } else if (total_time_ns.count() < 60000000000) {
54 std::cout << total_time_ns.count()/1000000000.0 << " s" << std::endl;
55 } else if (total_time_ns.count() < 3600000000000) {
56 std::cout << total_time_ns.count()/60000000000.0 << " min" << std::endl;
57 } else {
58 std::cout << total_time_ns.count()/3600000000000.0 << " h" << std::endl;
59 }
60 std::cout << std::endl;
61 }
62 };
63
64//------------------------------------------------------------------------------
66//------------------------------------------------------------------------------
68 private:
70 const std::string label;
72 std::map<size_t, std::chrono::high_resolution_clock::time_point> start;
74 std::map<size_t, std::chrono::high_resolution_clock::time_point> end;
76 std::mutex sync_start;
78 std::mutex sync_end;
79
80 public:
81//------------------------------------------------------------------------------
85//------------------------------------------------------------------------------
86 measure_diagnostic_threaded(const std::string message = "") :
87 label(message) {}
88
89//------------------------------------------------------------------------------
93//------------------------------------------------------------------------------
94 void start_time(const size_t thread_number) {
95 sync_start.lock();
96 start[thread_number] = std::chrono::high_resolution_clock::now();
97 sync_start.unlock();
98 }
99
100//------------------------------------------------------------------------------
104//------------------------------------------------------------------------------
105 void end_time(const size_t thread_number) {
106 const auto temp = std::chrono::high_resolution_clock::now();
107 sync_end.lock();
108 end[thread_number] = temp;
109 sync_end.unlock();
110 }
111
112//------------------------------------------------------------------------------
114//------------------------------------------------------------------------------
115 void print() {
116 std::chrono::nanoseconds total_time_ns = static_cast<std::chrono::nanoseconds> (0);
117 for (size_t i = 0, ie = start.size(); i < ie; i++) {
118 const auto duration = end[i] - start[i];
119 total_time_ns += std::chrono::duration_cast<std::chrono::nanoseconds> (duration);
120 }
121
122 std::cout << "Average " << label << " time : " << total_time_ns.count()/start.size() << " ns"<< std::endl;
123 }
124 };
125}
126
127#endif /* timing_h */
A timing object that averages over multiple threads.
Definition timing.hpp:67
void start_time(const size_t thread_number)
Start time for a given thread.
Definition timing.hpp:94
void print()
Print out the average time.
Definition timing.hpp:115
measure_diagnostic_threaded(const std::string message="")
Construct a time diagnostic object.
Definition timing.hpp:86
void end_time(const size_t thread_number)
End time for a given thread.
Definition timing.hpp:105
A timing object.
Definition timing.hpp:18
measure_diagnostic(const std::string message="")
Construct a time diagnostic object.
Definition timing.hpp:33
void print() const
Print the result.
Definition timing.hpp:39
Name space for timers.
Definition timing.hpp:14