Signal/Geometry Processing Library (SPL)  1.1.24
Timer.hpp
Go to the documentation of this file.
1 // Copyright (c) 2011 Michael D. Adams
2 // All rights reserved.
3 
4 // __START_OF_LICENSE__
5 //
6 // Copyright (c) 2015 Michael D. Adams
7 // All rights reserved.
8 //
9 // This file is part of the Signal Processing Library (SPL).
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3,
14 // or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public
22 // License along with this program; see the file LICENSE. If not,
23 // see <http://www.gnu.org/licenses/>.
24 //
25 // __END_OF_LICENSE__
26 
32 #ifndef SPL_Timer_hpp
33 #define SPL_Timer_hpp
34 
36 //
38 
39 #include <SPL/config.hpp>
40 #include <iostream>
41 #include <cstdlib>
42 #if defined(SPL_HAVE_SYS_TIME_H)
43 #include <sys/time.h>
44 #endif
45 
46 namespace SPL {
47 
53 // Timer Class
56 
61 class Timer
62 {
63 public:
64 
72  void start();
73 
81  void stop();
82 
96  double get() const;
97 
98 private:
99  // Do not look at private members. Doing so will make your eyes bleed!
100 #if defined(SPL_HAVE_GETTIMEOFDAY)
101  struct timeval startTime_;
102  struct timeval stopTime_;
103 #endif
104 };
105 
106 #if defined(SPL_HAVE_GETTIMEOFDAY)
107 
108 inline void Timer::start()
109 {
110  if (gettimeofday(&startTime_, 0)) {
111  // The gettimeofday function has failed.
112  // Ensure that the timer is initialized to some sane state.
113  std::cerr << "warning: gettimeofday failed\n";
114  startTime_.tv_sec = 0;
115  startTime_.tv_usec = 0;
116  }
117 }
118 
119 inline void Timer::stop()
120 {
121  if (gettimeofday(&stopTime_, 0)) {
122  // The gettimeofday function has failed.
123  // Ensure that the timer is initialized to some sane state.
124  std::cerr << "warning: gettimeofday failed\n";
125  stopTime_.tv_sec = 0;
126  stopTime_.tv_usec = 0;
127  }
128 }
129 
130 inline double Timer::get() const
131 {
132  double t0;
133  double t1;
134  t0 = ((double) startTime_.tv_sec) + ((double) startTime_.tv_usec) / 1e6;
135  t1 = ((double) stopTime_.tv_sec) + ((double) stopTime_.tv_usec) / 1e6;
136  return t1 - t0;
137 }
138 
139 #endif
140 
142 //
144 
156 double getCurrentMemUsage();
157 
168 double getPeakMemUsage();
169 
172 
177 }
178 
179 #endif
180 
double get() const
Get the timer value.
void stop()
Stop the timer.
Definition: Arcball.hpp:48
double getPeakMemUsage()
Get the peak memory usage for the process.
Definition: Timer.cpp:149
double getCurrentMemUsage()
Get the amount of memory currently being used by the process.
Definition: Timer.cpp:154
void start()
Start the timer.
A class for making timing measurements.
Definition: Timer.hpp:61