crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
SimpleHR.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2022 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * SimpleHR.hpp
24  *
25  * Simple high resolution timer for getting the time since creation in microseconds.
26  *
27  * Created on: Oct 17, 2018
28  * Author: ans
29  */
30 
31 #ifndef TIMER_SIMPLEHR_HPP_
32 #define TIMER_SIMPLEHR_HPP_
33 
34 #include "../Helper/DateTime.hpp"
35 
36 #include <chrono> // std::chrono
37 #include <string> // std::string
38 
39 namespace crawlservpp::Timer {
40 
41  /*
42  * DECLARATION
43  */
44 
46 
52  class SimpleHR {
53  public:
56 
57  SimpleHR();
58 
62 
63  std::uint64_t tick();
64  std::string tickStr();
65 
69 
70  void clear();
71 
73 
74  protected:
77 
79  std::chrono::high_resolution_clock::time_point timePoint;
80 
82  };
83 
84  /*
85  * IMPLEMENTATION
86  */
87 
89  inline SimpleHR::SimpleHR() : timePoint(std::chrono::high_resolution_clock::now()) {}
90 
92 
97  inline std::uint64_t SimpleHR::tick() {
98  const auto result{
99  std::chrono::duration_cast<std::chrono::microseconds>(
101  ).count()
102  };
103 
105 
106  return result;
107  }
108 
110 
118  inline std::string SimpleHR::tickStr() {
119  const auto result{
120  std::chrono::duration_cast<std::chrono::microseconds>(
122  ).count()
123  };
124 
126 
128  }
129 
131  inline void SimpleHR::clear() {
132  this->timePoint = std::chrono::high_resolution_clock::time_point{};
133  }
134 
135 } /* namespace crawlservpp::Timer */
136 
137 #endif /* TIMER_SIMPLEHR_HPP_ */
std::string tickStr()
Timer tick returning the number of microseconds passed as string.
Definition: SimpleHR.hpp:118
std::string microsecondsToString(std::uint64_t microseconds)
Converts microseconds into a well-formatted string.
Definition: DateTime.hpp:859
std::string now()
Formats the current date/time as string in the format YYYY-MM-DD HH:MM:SS.
Definition: DateTime.hpp:1045
A simple timer with high resolution.
Definition: SimpleHR.hpp:52
Namespace for timers.
Definition: Simple.hpp:40
SimpleHR()
Constructor starting the timer.
Definition: SimpleHR.hpp:89
std::chrono::high_resolution_clock::time_point timePoint
(Time) point of creation or last tick.
Definition: SimpleHR.hpp:79
std::uint64_t tick()
Timer tick returning the number of microseconds passed.
Definition: SimpleHR.hpp:97
void clear()
Resets the internal state of the timer.
Definition: SimpleHR.hpp:131