Quantum++  v0.8.2
C++11 quantum computing library
timer.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2015 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 
40 class Timer : public IDisplay
41 {
42 protected:
43  std::chrono::steady_clock::time_point _start, _end;
44 
45 public:
50  Timer() noexcept :
51  _start{std::chrono::steady_clock::now()}, _end{_start}
52  {
53  }
54 
60  void tic() noexcept
61  {
62  _start = _end = std::chrono::steady_clock::now();
63  }
64 
72  const Timer& toc() noexcept
73  {
74  _end = std::chrono::steady_clock::now();
75  return *this;
76  }
77 
84  double seconds() const noexcept
85  {
86  return std::chrono::duration_cast<std::chrono::duration<double>>(
87  _end - _start).count();
88  }
89 
93  Timer(const Timer&) = default;
94 
98  Timer(Timer&&) = default;
99 
103  Timer& operator=(const Timer&) = default;
104 
108  Timer& operator=(Timer&&) = default;
109 
113  virtual ~Timer() = default;
114 
115 private:
123  std::ostream& display(std::ostream& os) const override
124  {
125  return os << seconds();
126  }
127 }; /* class Timer */
128 
129 } /* namespace qpp */
130 
131 #endif /* CLASSES_TIMER_H_ */
const Timer & toc() noexcept
Stops the chronometer.
Definition: timer.h:72
Timer() noexcept
Constructs an instance with the current time as the starting point.
Definition: timer.h:50
std::chrono::steady_clock::time_point _end
Definition: timer.h:43
Quantum++ main namespace.
Definition: codes.h:30
virtual ~Timer()=default
Default virtual destructor.
Chronometer.
Definition: timer.h:40
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
std::chrono::steady_clock::time_point _start
Definition: timer.h:43
double seconds() const noexcept
Time passed in seconds.
Definition: timer.h:84
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override
Definition: timer.h:123
void tic() noexcept
Resets the chronometer.
Definition: timer.h:60