Quantum++  v0.8.8
C++11 quantum computing library
timer.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ 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 Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CLASSES_TIMER_H_
28 #define CLASSES_TIMER_H_
29 
30 namespace qpp
31 {
32 
42 template<typename T = std::chrono::duration<double>,
43  typename CLOCK_T = std::chrono::steady_clock>
44 class Timer : public IDisplay
45 {
46 protected:
47  typename CLOCK_T::time_point _start, _end;
48 
49 public:
54  Timer() noexcept :
55  _start{CLOCK_T::now()}, _end{_start}
56  {
57  }
58 
64  void tic() noexcept
65  {
66  _start = _end = CLOCK_T::now();
67  }
68 
76  const Timer& toc() noexcept
77  {
78  _end = CLOCK_T::now();
79  return *this;
80  }
81 
88  double tics() const noexcept
89  {
90  return std::chrono::duration_cast<T>(_end - _start).count();
91  }
92 
102  template<typename U = T>
103  U get_duration() const noexcept
104  {
105  return std::chrono::duration_cast<U>(_end - _start);
106  }
107 
111  Timer(const Timer&) = default;
112 
116  Timer(Timer&&) = default;
117 
121  Timer& operator=(const Timer&) = default;
122 
126  Timer& operator=(Timer&&) = default;
127 
131  virtual ~Timer() = default;
132 
133 private:
142  std::ostream& display(std::ostream& os) const override
143  {
144  return os << tics();
145  }
146 }; /* class Timer */
147 
148 } /* namespace qpp */
149 
150 #endif /* CLASSES_TIMER_H_ */
CLOCK_T::time_point _start
Definition: timer.h:47
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override
Definition: timer.h:142
Quantum++ main namespace.
Definition: codes.h:30
Chronometer.
Definition: timer.h:44
Timer() noexcept
Constructs an instance with the current time as the starting point.
Definition: timer.h:54
Timer & operator=(const Timer &)=default
Default copy assignment operator.
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:43
CLOCK_T::time_point _end
Definition: timer.h:47
virtual ~Timer()=default
Default virtual destructor.
double tics() const noexcept
Time passed in the duration specified by T.
Definition: timer.h:88
void tic() noexcept
Resets the chronometer.
Definition: timer.h:64
U get_duration() const noexcept
Duration specified by U.
Definition: timer.h:103
const Timer & toc() noexcept
Stops the chronometer.
Definition: timer.h:76