HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.0
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2016 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19#ifndef LIBSEMIGROUPS_SRC_TIMER_H_
20#define LIBSEMIGROUPS_SRC_TIMER_H_
21
22#include <chrono>
23#include <iostream>
24#include <string>
25
26// #include "to_string.h"
27
28namespace libsemigroups {
29
30// This is a simple class which can be used to send timing information in a
31// somewhat human readable format to the standard output.
32class Timer {
33 public:
34 // Default constructor, timer starts when object is created
35 Timer() : _start(std::chrono::high_resolution_clock::now()) {}
36
37 // Reset the timer (i.e. time from this point on)
38 void reset() { _start = std::chrono::high_resolution_clock::now(); }
39
40 // The elapsed time in nanoseconds since last reset
41 std::chrono::nanoseconds elapsed() const {
42 return std::chrono::duration_cast<std::chrono::nanoseconds>(
43 std::chrono::high_resolution_clock::now() - _start);
44 }
45
46 // String containing the somewhat human readable amount of time, this is
47 // primarily intended for testing purposes
48 std::string string(std::chrono::nanoseconds elapsed) const {
49 std::string out;
50 if (string_it<std::chrono::hours>(out, elapsed, "h ", 0)) {
51 string_it<std::chrono::minutes>(out, elapsed, "m", 0);
52 return out;
53 } else if (string_it<std::chrono::minutes>(out, elapsed, "m ", 0)) {
54 string_it<std::chrono::seconds>(out, elapsed, "s", 0);
55 return out;
56 } else if (string_it<std::chrono::milliseconds>(out, elapsed, "ms",
57 9)) {
58 return out;
59 } else if (string_it<std::chrono::microseconds>(out, elapsed, "\u03BCs",
60 9)) {
61 return out;
62 } else if (string_it<std::chrono::nanoseconds>(out, elapsed, "ns", 0)) {
63 return out;
64 }
65 return out;
66 }
67
68 // String containing the somewhat human readable amount of time since the
69 // last reset
70 std::string string() const { return string(elapsed()); }
71
72 // Left shift the string containing the somewhat human readable amount of
73 // time since last reset to an ostream
74 friend std::ostream &operator<<(std::ostream &os, Timer const &t) {
75 os << t.string();
76 return os;
77 }
78
79 private:
80 std::chrono::high_resolution_clock::time_point _start;
81
82 template <typename T>
83 bool string_it(std::string &str, std::chrono::nanoseconds &elapsed,
84 std::string unit, size_t threshold) const {
85 T x = std::chrono::duration_cast<T>(elapsed);
86 if (x > T(threshold)) {
87 str += std::to_string(x.count()) + unit;
88 elapsed -= x;
89 return true;
90 }
91 return false;
92 }
93};
94} // namespace libsemigroups
95
96#endif // LIBSEMIGROUPS_SRC_TIMER_H_
Definition timer.h:32
void reset()
Definition timer.h:38
Timer()
Definition timer.h:35
std::string string() const
Definition timer.h:70
friend std::ostream & operator<<(std::ostream &os, Timer const &t)
Definition timer.h:74
std::chrono::nanoseconds elapsed() const
Definition timer.h:41
std::string string(std::chrono::nanoseconds elapsed) const
Definition timer.h:48
Definition timer.h:28
Definition bmat8.hpp:364
std::string to_string(HPCombi::epu8 const &a)
Definition epu8_impl.hpp:550