DGtal  0.9.2
Clock.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file Clock.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
21  *
22  * @date 2009/12/11
23  *
24  * Implementation of methods defined in Clock.h
25  *
26  * This file is part of the DGtal library (backported from Imagene)
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 // class Clock
34 //////////////////////////////////////////////////////////////////////////////
35 
36 /**
37  * Constructor.
38  */
39 inline
40 DGtal::Clock::Clock()
41 {
42 }
43 
44 
45 //- Starts a clock.
46 inline
47 void
48 DGtal::Clock::startClock()
49 {
50 
51 #ifdef WIN32
52  myFirstTick = clock();
53  if (myFirstTick == (clock_t) -1)
54  {
55  std::cerr << "[Clock::startClock] Error: can't start clock." << std::endl;
56  }
57 #else
58 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
59  clock_serv_t cclock;
60  mach_timespec_t mts;
61  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
62  clock_get_time(cclock, &mts);
63  mach_port_deallocate(mach_task_self(), cclock);
64  myTimerStart.tv_sec = mts.tv_sec;
65  myTimerStart.tv_nsec = mts.tv_nsec;
66 #else
67  clock_gettime(CLOCK_REALTIME, &myTimerStart);
68 #endif
69 #endif
70 }
71 
72 
73 //- @return the time (in ms) since the last 'startClock()' or 'restartClock()'.
74 inline
75 double
76 DGtal::Clock::stopClock() const
77 {
78 
79 #ifdef WIN32
80  clock_t last_tick = clock();
81  if (last_tick == (clock_t) -1)
82  {
83  std::cerr << "[Clock::stopClock] Error: can't stop clock." << std::endl;
84  }
85  return (double) ((double) 1000.0 * (double)(last_tick - myFirstTick)
86  / (double) CLOCKS_PER_SEC);
87 #else
88  struct timespec current;
89 
90 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
91  clock_serv_t cclock;
92  mach_timespec_t mts;
93  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
94  clock_get_time(cclock, &mts);
95  mach_port_deallocate(mach_task_self(), cclock);
96  current.tv_sec = mts.tv_sec;
97  current.tv_nsec = mts.tv_nsec;
98 #else
99  clock_gettime(CLOCK_REALTIME, &current); //Linux gettime
100 #endif
101 
102  return (( current.tv_sec - myTimerStart.tv_sec) *1000 +
103  ( current.tv_nsec - myTimerStart.tv_nsec)/1000000.0);
104 #endif
105 
106 }
107 
108 inline
109 double
110 DGtal::Clock::restartClock()
111 {
112 
113 #ifdef WIN32
114  clock_t last_tick = clock();
115  if (last_tick == (clock_t) -1)
116  {
117  std::cerr << "[Clock::stopClock] Error: can't restart clock." << std::endl;
118  }
119  const double delta = ((double) 1000.0 * (double)(last_tick - myFirstTick)
120  / (double) CLOCKS_PER_SEC);
121  myFirstTick = last_tick;
122  return delta;
123 #else
124  struct timespec current;
125 
126 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
127  clock_serv_t cclock;
128  mach_timespec_t mts;
129  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
130  clock_get_time(cclock, &mts);
131  mach_port_deallocate(mach_task_self(), cclock);
132  current.tv_sec = mts.tv_sec;
133  current.tv_nsec = mts.tv_nsec;
134 #else
135  clock_gettime(CLOCK_REALTIME, &current); //Linux gettime
136 #endif
137 
138  const double delta = (( current.tv_sec - myTimerStart.tv_sec) *1000 +
139  ( current.tv_nsec - myTimerStart.tv_nsec)/1000000.0);
140  myTimerStart.tv_sec = current.tv_sec;
141  myTimerStart.tv_nsec = current.tv_nsec;
142  return delta;
143 #endif
144 
145 }
146 
147 
148 /**
149  * Destructor.
150  */
151 inline
152 DGtal::Clock::~Clock()
153 {
154 }
155 
156 
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 // Interface - public :
160 
161 /**
162  * Writes/Displays the object on an output stream.
163  * @param out the output stream where the object is written.
164  */
165 inline
166 void
167 DGtal::Clock::selfDisplay( std::ostream & out ) const
168 {
169  out << "[Clock]";
170 }
171 
172 /**
173  * Checks the validity/consistency of the object.
174  * @return 'true' if the object is valid, 'false' otherwise.
175  */
176 inline
177 bool
178 DGtal::Clock::isValid() const
179 {
180  return true;
181 }
182 
183