File: timing.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (175 lines) | stat: -rw-r--r-- 4,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*!
 * \file
 * \brief Definitions of Timing classes
 * \author Thomas Eriksson, Tony Ottosson and Tobias Ringstrom
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef TIMING_H
#define TIMING_H


namespace itpp {

/*!
  \addtogroup timers
*/

/*!
  \brief A virtual base class for timers.
  \ingroup timers
*/
class Timer {
public:
    //! Create a new timer.  Sets the time to zero.
    Timer();
    //! Virtual destructor
    virtual ~Timer() { }
    //! Start the timer.  This does not set the time to zero.
    void start(void);
    //! Stop the timer.  Returns the elapsed time in seconds.
    double stop(void);
    //! Sets the time to time t, which is zero by default. Stops the timer if it is running.
    void reset(double t=0.0);
    //! Resets the timer and starts it.
    void tic(void);
    //! Returns the elapsed time since last tic()
    double toc(void);
    //! Prints the elapsed time since last tic()
    void toc_print(void);
    //! Returns the elapsed time.
    double get_time() const;

protected:
    //! Vitrual function that returns teh current time
    virtual double get_current_time() const = 0;
    //! The start time of the timer
    double start_time;
    //! The stop time of the timer
    double stop_time;
    //! The ellapsed time from start to stop
    double elapsed_time;
    //! A bool that indicates if the timer is running or not
    bool running;
};

/*!
  \brief A CPU time timer class
  \ingroup timers

  Measures the time spent by the CPU on the current process.  If two processes
  are running concurrently, one real seconds equal 5 CPU seconds per process.
  The resolution is not very good (in the order of 0.01 seconds).

  Usage: Define a time object:
  \code
  CPU_Timer timer;
  \endcode

  Actions:
  Reset: \code timer.reset(); \endcode
  Start: \code timer.start(); \endcode
  Stop: \code timer.stop(); \endcode
  Get time: \code elapsedtime = timer.get_time(); \endcode

  It is possible to get elapsed time without stopping the timer.
  Observe that it is also possible to use the macros "time.tic();"
  to reset and start clock and "time.toc();" stop and print the elapsed time.
  \warning May give an negative answer if the measured time is too long.
*/
class CPU_Timer : public Timer {
public:
    //! Create a new timer. Sets the time to zero.
    CPU_Timer() { }

protected:
    //!
    double get_current_time() const;
};

/*!
  \brief A real time timer class
  \ingroup timers

  Measures real time.

  Usage: Define a time object:
  \code
  Real_Timer timer;
  \endcode

  Actions:
  Reset: \code timer.reset(); \endcode
  Start: \code timer.start(); \endcode
  Stop: \code timer.stop(); \endcode
  Get time: \code elapsedtime = timer.get_time(); \endcode

  It is possible to get elapsed time without stopping the timer.
  Observe that it is also possible to use the macros "time.tic();"
  to reset and start clock and "time.toc_print();" to print the elapsed time.

  \warning May give an negative answer if the measured time is too long.
*/
class Real_Timer : public Timer {
public:
    //! Create a new timer. Sets the time to zero.
    Real_Timer() { }

protected:
    //!
    double get_current_time() const;
};

/*!
  \brief Reset and start timer
  \ingroup timers
*/
void tic();

/*!
  \brief Returns the elapsed time since last tic()
  \ingroup timers
*/
double toc();

/*!
  \brief Prints the elapsed time since last tic()
  \ingroup timers
*/
void toc_print();

/*!
  \brief pause
  \ingroup timers

  \code pause(n); \endcode  Pauses for n seconds before continuing
  \code pause(); \endcode  Pauses until a key is pressed

*/
void pause(double t=-1);

} // namespace itpp

#endif // #ifndef TIMING_H