DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Clock.ih
1 
29 
30 
31 
32 #include "DGtal/base/Clock.h"
34 
35 using namespace std;
36 using namespace DGtal;
37 
39 // class Clock
41 
43 // Standard services - public :
44 
46 // -------------------------- timing services -------------------------------
47 
48 
49 
50 
54 inline
56 {
57 }
58 
59 
60 //- Starts a clock.
61 inline
62 void
64 {
65 
66 #if ( (defined(WIN32)) )
67  myFirstTick = clock();
68  if (myFirstTick == (clock_t) -1)
69  {
70  cerr << "[Clock::startClock] Erreur sur 'clock()'." << endl;
71  }
72 #else
73 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
74  clock_serv_t cclock;
75  mach_timespec_t mts;
76  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
77  clock_get_time(cclock, &mts);
78  mach_port_deallocate(mach_task_self(), cclock);
79  myTimerStart.tv_sec = mts.tv_sec;
80  myTimerStart.tv_nsec = mts.tv_nsec;
81 #else
82  clock_gettime(CLOCK_REALTIME, &myTimerStart);
83 #endif
84 #endif
85 }
86 
87 
88 //- @return the time (in ms) since the last 'startClock()'.
89 inline
90 double
92 {
93 
94 #if ( (defined(WIN32)) )
95  clock_t last_tick = clock();
96  if (last_tick == (clock_t) -1)
97  {
98  cerr << "[Clock::stopClock] Erreur sur 'clock()'." << endl;
99  }
100  return (double) ((double) 1000.0 * (double)(last_tick - myFirstTick)
101  / (double) CLOCKS_PER_SEC);
102 #else
103  struct timespec current;
104 
105 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
106  clock_serv_t cclock;
107  mach_timespec_t mts;
108  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
109  clock_get_time(cclock, &mts);
110  mach_port_deallocate(mach_task_self(), cclock);
111  current.tv_sec = mts.tv_sec;
112  current.tv_nsec = mts.tv_nsec;
113 #else
114  clock_gettime(CLOCK_REALTIME, &current); //Linux gettime
115 #endif
116 
117 
118  return (( current.tv_sec - myTimerStart.tv_sec) *1000 +
119  ( current.tv_nsec - myTimerStart.tv_nsec)/1000000.0);
120 #endif
121 
122 }
123 
124 
125 
129 inline
131 {
132 }
133 
134 
135 
137 // Interface - public :
138 
143 inline
144 void
145 DGtal::Clock::selfDisplay( std::ostream & out ) const
146 {
147  out << "[Clock]";
148 }
149 
154 inline
155 bool
157 {
158  return true;
159 }
160 
161