Quantum++  v1.0-rc3
A modern C++11 quantum computing library
timer.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2018 Vlad Gheorghiu (vgheorgh@gmail.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef CLASSES_TIMER_H_
33 #define CLASSES_TIMER_H_
34 
35 namespace qpp {
45 template <typename T = std::chrono::duration<double>,
46  typename CLOCK_T = std::chrono::steady_clock>
47 class Timer : public IDisplay {
48  protected:
49  typename CLOCK_T::time_point start_, end_;
50 
51  public:
56  Timer() noexcept : start_{CLOCK_T::now()}, end_{start_} {}
57 
63  void tic() noexcept { start_ = end_ = CLOCK_T::now(); }
64 
72  const Timer& toc() noexcept {
73  end_ = CLOCK_T::now();
74  return *this;
75  }
76 
83  double tics() const noexcept {
84  return std::chrono::duration_cast<T>(end_ - start_).count();
85  }
86 
96  template <typename U = T>
97  U get_duration() const noexcept {
98  return std::chrono::duration_cast<U>(end_ - start_);
99  }
100 
104  Timer(const Timer&) = default;
105 
109  Timer(Timer&&) = default;
110 
114  Timer& operator=(const Timer&) = default;
115 
119  Timer& operator=(Timer&&) = default;
120 
124  virtual ~Timer() = default;
125 
126  private:
135  std::ostream& display(std::ostream& os) const override {
136  return os << tics();
137  }
138 }; /* class Timer */
139 
140 } /* namespace qpp */
141 
142 #endif /* CLASSES_TIMER_H_ */
CLOCK_T::time_point end_
Definition: timer.h:49
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override
Definition: timer.h:135
Quantum++ main namespace.
Definition: codes.h:35
Chronometer.
Definition: timer.h:47
Timer() noexcept
Constructs an instance with the current time as the starting point.
Definition: timer.h:56
Timer & operator=(const Timer &)=default
Default copy assignment operator.
CLOCK_T::time_point start_
Definition: timer.h:49
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:46
virtual ~Timer()=default
Default virtual destructor.
double tics() const noexcept
Time passed in the duration specified by T.
Definition: timer.h:83
void tic() noexcept
Resets the chronometer.
Definition: timer.h:63
U get_duration() const noexcept
Duration specified by U.
Definition: timer.h:97
const Timer & toc() noexcept
Stops the chronometer.
Definition: timer.h:72