Quantum++  v1.0-rc2
A modern C++11 quantum computing library
timer.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 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 {
41 template<typename T = std::chrono::duration<double>,
42  typename CLOCK_T = std::chrono::steady_clock>
43 class Timer : public IDisplay
44 {
45 protected:
46  typename CLOCK_T::time_point start_, end_;
47 
48 public:
53  Timer() noexcept :
54  start_{CLOCK_T::now()}, end_{start_}
55  {
56  }
57 
63  void tic() noexcept
64  {
65  start_ = end_ = CLOCK_T::now();
66  }
67 
75  const Timer& toc() noexcept
76  {
77  end_ = CLOCK_T::now();
78  return *this;
79  }
80 
87  double tics() const noexcept
88  {
89  return std::chrono::duration_cast<T>(end_ - start_).count();
90  }
91 
101  template<typename U = T>
102  U get_duration() const noexcept
103  {
104  return std::chrono::duration_cast<U>(end_ - start_);
105  }
106 
110  Timer(const Timer&) = default;
111 
115  Timer(Timer&&) = default;
116 
120  Timer& operator=(const Timer&) = default;
121 
125  Timer& operator=(Timer&&) = default;
126 
130  virtual ~Timer() = default;
131 
132 private:
141  std::ostream& display(std::ostream& os) const override
142  {
143  return os << tics();
144  }
145 }; /* class Timer */
146 
147 } /* namespace qpp */
148 
149 #endif /* CLASSES_TIMER_H_ */
CLOCK_T::time_point end_
Definition: timer.h:46
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override
Definition: timer.h:141
Quantum++ main namespace.
Definition: codes.h:30
Chronometer.
Definition: timer.h:43
Timer() noexcept
Constructs an instance with the current time as the starting point.
Definition: timer.h:53
Timer & operator=(const Timer &)=default
Default copy assignment operator.
CLOCK_T::time_point start_
Definition: timer.h:46
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:42
virtual ~Timer()=default
Default virtual destructor.
double tics() const noexcept
Time passed in the duration specified by T.
Definition: timer.h:87
void tic() noexcept
Resets the chronometer.
Definition: timer.h:63
U get_duration() const noexcept
Duration specified by U.
Definition: timer.h:102
const Timer & toc() noexcept
Stops the chronometer.
Definition: timer.h:75