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.
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.
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/>.
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
24 * Implementation of methods defined in Clock.h
26 * This file is part of the DGtal library (backported from Imagene)
29///////////////////////////////////////////////////////////////////////////////
30///////////////////////////////////////////////////////////////////////////////
32///////////////////////////////////////////////////////////////////////////////
34//////////////////////////////////////////////////////////////////////////////
48DGtal::Clock::startClock()
52 myFirstTick = clock();
53 if (myFirstTick == (clock_t) -1)
55 std::cerr << "[Clock::startClock] Error: can't start clock." << std::endl;
58#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
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;
67 clock_gettime(CLOCK_REALTIME, &myTimerStart);
73//- @return the time (in ms) since the last 'startClock()' or 'restartClock()'.
76DGtal::Clock::stopClock() const
80 clock_t last_tick = clock();
81 if (last_tick == (clock_t) -1)
83 std::cerr << "[Clock::stopClock] Error: can't stop clock." << std::endl;
85 return (double) ((double) 1000.0 * (double)(last_tick - myFirstTick)
86 / (double) CLOCKS_PER_SEC);
88 struct timespec current;
90#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
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;
99 clock_gettime(CLOCK_REALTIME, ¤t); //Linux gettime
102 return (( current.tv_sec - myTimerStart.tv_sec) *1000 +
103 ( current.tv_nsec - myTimerStart.tv_nsec)/1000000.0);
110DGtal::Clock::restartClock()
114 clock_t last_tick = clock();
115 if (last_tick == (clock_t) -1)
117 std::cerr << "[Clock::stopClock] Error: can't restart clock." << std::endl;
119 const double delta = ((double) 1000.0 * (double)(last_tick - myFirstTick)
120 / (double) CLOCKS_PER_SEC);
121 myFirstTick = last_tick;
124 struct timespec current;
126#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
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;
135 clock_gettime(CLOCK_REALTIME, ¤t); //Linux gettime
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;
152DGtal::Clock::~Clock()
158///////////////////////////////////////////////////////////////////////////////
159// Interface - public :
162 * Writes/Displays the object on an output stream.
163 * @param out the output stream where the object is written.
167DGtal::Clock::selfDisplay( std::ostream & out ) const
173 * Checks the validity/consistency of the object.
174 * @return 'true' if the object is valid, 'false' otherwise.
178DGtal::Clock::isValid() const