Quantum++  v0.7
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
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 
98  template<typename charT, typename traits>
99  friend std::basic_ostream<charT, traits>&
100  operator<<(std::basic_ostream<charT, traits>& os, const Timer& rhs)
101  {
102  return os << rhs.seconds();
103  }
104 }; /* class Timer */
105 
106 } /* namespace qpp */
107 
108 #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
Measures time.
Definition: timer.h:40
std::chrono::steady_clock::time_point _start
Definition: timer.h:43
double seconds() const noexcept
Time passed in seconds.
Definition: timer.h:84
void tic() noexcept
Resets the chronometer.
Definition: timer.h:60