| 1 | /*! |
|---|
| 2 | * \file |
|---|
| 3 | * \brief Definitions of Timing classes |
|---|
| 4 | * \author Thomas Eriksson, Tony Ottosson and Tobias Ringstrom |
|---|
| 5 | * |
|---|
| 6 | * ------------------------------------------------------------------------- |
|---|
| 7 | * |
|---|
| 8 | * IT++ - C++ library of mathematical, signal processing, speech processing, |
|---|
| 9 | * and communications classes and functions |
|---|
| 10 | * |
|---|
| 11 | * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) |
|---|
| 12 | * |
|---|
| 13 | * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * This program is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with this program; if not, write to the Free Software |
|---|
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 26 | * |
|---|
| 27 | * ------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef TIMING_H |
|---|
| 31 | #define TIMING_H |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | namespace itpp { |
|---|
| 35 | |
|---|
| 36 | /*! |
|---|
| 37 | \addtogroup timers |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | /*! |
|---|
| 41 | \brief A virtual base class for timers. |
|---|
| 42 | \ingroup timers |
|---|
| 43 | */ |
|---|
| 44 | class Timer { |
|---|
| 45 | public: |
|---|
| 46 | //! Create a new timer. Sets the time to zero. |
|---|
| 47 | Timer(); |
|---|
| 48 | //! Virtual destructor |
|---|
| 49 | virtual ~Timer() { } |
|---|
| 50 | //! Start the timer. This does not set the time to zero. |
|---|
| 51 | void start(void); |
|---|
| 52 | //! Stop the timer. Returns the elapsed time in seconds. |
|---|
| 53 | double stop(void); |
|---|
| 54 | //! Sets the time to time t, which is zero by default. Stops the timer if it is running. |
|---|
| 55 | void reset(double t=0.0); |
|---|
| 56 | //! Resets the timer and starts it. |
|---|
| 57 | void tic(void); |
|---|
| 58 | //! Returns the elapsed time since last tic() |
|---|
| 59 | double toc(void); |
|---|
| 60 | //! Prints the elapsed time since last tic() |
|---|
| 61 | void toc_print(void); |
|---|
| 62 | //! Returns the elapsed time. |
|---|
| 63 | double get_time() const; |
|---|
| 64 | |
|---|
| 65 | protected: |
|---|
| 66 | //! Vitrual function that returns teh current time |
|---|
| 67 | virtual double get_current_time() const = 0; |
|---|
| 68 | //! The start time of the timer |
|---|
| 69 | double start_time; |
|---|
| 70 | //! The stop time of the timer |
|---|
| 71 | double stop_time; |
|---|
| 72 | //! The ellapsed time from start to stop |
|---|
| 73 | double elapsed_time; |
|---|
| 74 | //! A bool that indicates if the timer is running or not |
|---|
| 75 | bool running; |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | /*! |
|---|
| 79 | \brief A CPU time timer class |
|---|
| 80 | \ingroup timers |
|---|
| 81 | |
|---|
| 82 | Measures the time spent by the CPU on the current process. If two processes |
|---|
| 83 | are running concurrently, one real seconds equal 5 CPU seconds per process. |
|---|
| 84 | The resolution is not very good (in the order of 0.01 seconds). |
|---|
| 85 | |
|---|
| 86 | Usage: Define a time object: |
|---|
| 87 | \code |
|---|
| 88 | CPU_Timer timer; |
|---|
| 89 | \endcode |
|---|
| 90 | |
|---|
| 91 | Actions: |
|---|
| 92 | Reset: \code timer.reset(); \endcode |
|---|
| 93 | Start: \code timer.start(); \endcode |
|---|
| 94 | Stop: \code timer.stop(); \endcode |
|---|
| 95 | Get time: \code elapsedtime = timer.get_time(); \endcode |
|---|
| 96 | |
|---|
| 97 | It is possible to get elapsed time without stopping the timer. |
|---|
| 98 | Observe that it is also possible to use the macros "time.tic();" |
|---|
| 99 | to reset and start clock and "time.toc();" stop and print the elapsed time. |
|---|
| 100 | \warning May give an negative answer if the measured time is too long. |
|---|
| 101 | */ |
|---|
| 102 | class CPU_Timer : public Timer { |
|---|
| 103 | public: |
|---|
| 104 | //! Create a new timer. Sets the time to zero. |
|---|
| 105 | CPU_Timer() { } |
|---|
| 106 | |
|---|
| 107 | protected: |
|---|
| 108 | //! |
|---|
| 109 | double get_current_time() const; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | /*! |
|---|
| 113 | \brief A real time timer class |
|---|
| 114 | \ingroup timers |
|---|
| 115 | |
|---|
| 116 | Measures real time. |
|---|
| 117 | |
|---|
| 118 | Usage: Define a time object: |
|---|
| 119 | \code |
|---|
| 120 | Real_Timer timer; |
|---|
| 121 | \endcode |
|---|
| 122 | |
|---|
| 123 | Actions: |
|---|
| 124 | Reset: \code timer.reset(); \endcode |
|---|
| 125 | Start: \code timer.start(); \endcode |
|---|
| 126 | Stop: \code timer.stop(); \endcode |
|---|
| 127 | Get time: \code elapsedtime = timer.get_time(); \endcode |
|---|
| 128 | |
|---|
| 129 | It is possible to get elapsed time without stopping the timer. |
|---|
| 130 | Observe that it is also possible to use the macros "time.tic();" |
|---|
| 131 | to reset and start clock and "time.toc_print();" to print the elapsed time. |
|---|
| 132 | |
|---|
| 133 | \warning May give an negative answer if the measured time is too long. |
|---|
| 134 | */ |
|---|
| 135 | class Real_Timer : public Timer { |
|---|
| 136 | public: |
|---|
| 137 | //! Create a new timer. Sets the time to zero. |
|---|
| 138 | Real_Timer() { } |
|---|
| 139 | |
|---|
| 140 | protected: |
|---|
| 141 | //! |
|---|
| 142 | double get_current_time() const; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | /*! |
|---|
| 146 | \brief Reset and start timer |
|---|
| 147 | \ingroup timers |
|---|
| 148 | */ |
|---|
| 149 | void tic(); |
|---|
| 150 | |
|---|
| 151 | /*! |
|---|
| 152 | \brief Returns the elapsed time since last tic() |
|---|
| 153 | \ingroup timers |
|---|
| 154 | */ |
|---|
| 155 | double toc(); |
|---|
| 156 | |
|---|
| 157 | /*! |
|---|
| 158 | \brief Prints the elapsed time since last tic() |
|---|
| 159 | \ingroup timers |
|---|
| 160 | */ |
|---|
| 161 | void toc_print(); |
|---|
| 162 | |
|---|
| 163 | /*! |
|---|
| 164 | \brief pause |
|---|
| 165 | \ingroup timers |
|---|
| 166 | |
|---|
| 167 | \code pause(n); \endcode Pauses for n seconds before continuing |
|---|
| 168 | \code pause(); \endcode Pauses until a key is pressed |
|---|
| 169 | |
|---|
| 170 | */ |
|---|
| 171 | void pause(double t=-1); |
|---|
| 172 | |
|---|
| 173 | } // namespace itpp |
|---|
| 174 | |
|---|
| 175 | #endif // #ifndef TIMING_H |
|---|