]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stopwatch.cpp
4ff9b7532d648b86a854c360ed46aa1a26e8479b
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stopwatch.cpp
3 // Purpose: wxStopWatch and other non-GUI stuff from wx/timer.h
5 // Original version by Julian Smart
6 // Vadim Zeitlin got rid of all ifdefs (11.12.99)
7 // Sylvain Bougnoux added wxStopWatch class
8 // Guillermo Rodriguez <guille@iies.es> rewrote from scratch (Dic/99)
10 // Created: 20.06.2003 (extracted from common/timercmn.cpp)
12 // Copyright: (c) 1998-2003 wxWidgets Team
13 // Licence: wxWindows licence
14 ///////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/stopwatch.h"
35 #include "wx/msw/wrapwin.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #if defined(__WIN32__) && !defined(HAVE_FTIME) && !defined(__MWERKS__) && !defined(__WXWINCE__)
49 #if defined(__VISAGECPP__) && !defined(HAVE_FTIME)
51 # if __IBMCPP__ >= 400
52 # define ftime(x) _ftime(x)
56 #if defined(__MWERKS__) && defined(__WXMSW__)
58 # undef HAVE_GETTIMEOFDAY
65 #include "wx/msw/private.h"
66 #include "wx/msw/wince/time.h"
68 #endif // __WXPALMOS5__
71 #if !defined(__WXMAC__) && !defined(__WXWINCE__)
72 #include <sys/types.h> // for time_t
75 #if defined(HAVE_GETTIMEOFDAY)
78 #elif defined(HAVE_FTIME)
79 #include <sys/timeb.h>
85 #include <SystemMgr.h>
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
110 bool CanBeUsed() const
112 return freq
.QuadPart
!= 0;
115 wxCriticalSection cs
;
122 const int MILLISECONDS_PER_SECOND
= 1000;
123 const int MICROSECONDS_PER_SECOND
= 1000*1000;
125 } // anonymous namespace
127 void wxStopWatch::DoStart()
130 if ( !gs_perfCounter
.init
)
132 wxCriticalSectionLocker
lock(gs_perfCounter
.cs
);
133 ::QueryPerformanceFrequency(&gs_perfCounter
.freq
);
135 // Just a sanity check: it's not supposed to happen but verify that
136 // ::QueryPerformanceCounter() succeeds so that we can really use it.
137 LARGE_INTEGER counter
;
138 if ( !::QueryPerformanceCounter(&counter
) )
140 wxLogDebug("QueryPerformanceCounter() unexpected failed (%s), "
141 "will not use it.", wxSysErrorMsg());
143 gs_perfCounter
.freq
.QuadPart
= 0;
146 gs_perfCounter
.init
= true;
150 m_t0
= GetCurrentClockValue();
153 wxLongLong
wxStopWatch::GetClockFreq() const
156 // Under MSW we use the high resolution performance counter timer which has
157 // its own frequency (usually related to the CPU clock speed).
158 if ( gs_perfCounter
.CanBeUsed() )
159 return gs_perfCounter
.freq
.QuadPart
;
162 // Currently milliseconds are used everywhere else.
163 return MILLISECONDS_PER_SECOND
;
166 void wxStopWatch::Start(long t0
)
170 m_t0
-= (wxLongLong(t0
)*GetClockFreq())/MILLISECONDS_PER_SECOND
;
173 wxLongLong
wxStopWatch::GetCurrentClockValue() const
176 if ( gs_perfCounter
.CanBeUsed() )
178 LARGE_INTEGER counter
;
179 ::QueryPerformanceCounter(&counter
);
180 return counter
.QuadPart
;
184 return wxGetLocalTimeMillis();
187 wxLongLong
wxStopWatch::TimeInMicro() const
189 const wxLongLong
elapsed(m_pauseCount
? m_elapsedBeforePause
190 : GetCurrentClockValue() - m_t0
);
192 return (elapsed
*MICROSECONDS_PER_SECOND
)/GetClockFreq();
195 #endif // wxUSE_STOPWATCH
197 // ----------------------------------------------------------------------------
198 // old timer functions superceded by wxStopWatch
199 // ----------------------------------------------------------------------------
203 static wxLongLong wxStartTime
= 0l;
205 // starts the global timer
208 wxStartTime
= wxGetLocalTimeMillis();
211 // Returns elapsed time in milliseconds
212 long wxGetElapsedTime(bool resetTimer
)
214 wxLongLong oldTime
= wxStartTime
;
215 wxLongLong newTime
= wxGetLocalTimeMillis();
218 wxStartTime
= newTime
;
220 return (newTime
- oldTime
).GetLo();
223 #endif // wxUSE_LONGLONG
225 // ----------------------------------------------------------------------------
226 // the functions to get the current time and timezone info
227 // ----------------------------------------------------------------------------
229 // Get local time as seconds since 00:00:00, Jan 1st 1970
230 long wxGetLocalTime()
235 // This cannot be made static because mktime can overwrite it.
237 memset(&tm
, 0, sizeof(tm
));
240 tm
.tm_mday
= 5; // not Jan 1st 1970 due to mktime 'feature'
244 tm
.tm_isdst
= -1; // let mktime guess
246 // Note that mktime assumes that the struct tm contains local time.
248 t1
= time(&t1
); // now
249 t0
= mktime(&tm
); // origin
251 // Return the difference in seconds.
253 if (( t0
!= (time_t)-1 ) && ( t1
!= (time_t)-1 ))
254 return (long)difftime(t1
, t0
) + (60 * 60 * 24 * 4);
256 wxLogSysError(_("Failed to get the local system time"));
260 // Get UTC time as seconds since 00:00:00, Jan 1st 1970
263 return (long)time(NULL
);
268 // Get local time as milliseconds since 00:00:00, Jan 1st 1970
269 wxLongLong
wxGetLocalTimeMillis()
271 wxLongLong val
= 1000l;
273 // If possible, use a function which avoids conversions from
274 // broken-up time structures to milliseconds
276 #if defined(__WXPALMOS__)
285 uint32_t now
= TimGetSeconds();
286 uint32_t then
= TimDateTimeToSeconds (&thenst
);
287 return SysTimeToMilliSecs(SysTimeInSecs(now
- then
));
288 #elif defined(__WXMSW__) && (defined(__WINE__) || defined(__MWERKS__))
289 // This should probably be the way all WXMSW compilers should do it
290 // Go direct to the OS for time
292 SYSTEMTIME thenst
= { 1970, 1, 4, 1, 0, 0, 0, 0 }; // 00:00:00 Jan 1st 1970
294 SystemTimeToFileTime( &thenst
, &thenft
);
295 wxLongLong
then( thenft
.dwHighDateTime
, thenft
.dwLowDateTime
); // time in 100 nanoseconds
298 GetLocalTime( &nowst
);
300 SystemTimeToFileTime( &nowst
, &nowft
);
301 wxLongLong
now( nowft
.dwHighDateTime
, nowft
.dwLowDateTime
); // time in 100 nanoseconds
303 return ( now
- then
) / 10000.0; // time from 00:00:00 Jan 1st 1970 to now in milliseconds
305 #elif defined(HAVE_GETTIMEOFDAY)
307 if ( wxGetTimeOfDay(&tp
) != -1 )
310 return (val
+ (tp
.tv_usec
/ 1000));
314 wxLogError(_("wxGetTimeOfDay failed."));
317 #elif defined(HAVE_FTIME)
320 // ftime() is void and not int in some mingw32 headers, so don't
321 // test the return code (well, it shouldn't fail anyhow...)
324 return (val
+ tp
.millitm
);
325 #else // no gettimeofday() nor ftime()
326 // We use wxGetLocalTime() to get the seconds since
327 // 00:00:00 Jan 1st 1970 and then whatever is available
328 // to get millisecond resolution.
330 // NOTE that this might lead to a problem if the clocks
331 // use different sources, so this approach should be
332 // avoided where possible.
334 val
*= wxGetLocalTime();
336 // GRG: This will go soon as all WIN32 seem to have ftime
337 // JACS: unfortunately not. WinCE doesn't have it.
338 #if defined (__WIN32__)
339 // If your platform/compiler needs to use two different functions
340 // to get ms resolution, please do NOT just shut off these warnings,
341 // drop me a line instead at <guille@iies.es>
345 #warning "Possible clock skew bug in wxGetLocalTimeMillis()!"
350 val
+= st
.wMilliseconds
;
352 // If your platform/compiler does not support ms resolution please
353 // do NOT just shut off these warnings, drop me a line instead at
356 #if defined(__VISUALC__) || defined (__WATCOMC__)
357 #pragma message("wxStopWatch will be up to second resolution!")
358 #elif defined(__BORLANDC__)
359 #pragma message "wxStopWatch will be up to second resolution!"
361 #warning "wxStopWatch will be up to second resolution!"
367 #endif // time functions
370 #else // !wxUSE_LONGLONG
372 double wxGetLocalTimeMillis(void)
374 return (double(clock()) / double(CLOCKS_PER_SEC
)) * 1000.0;
377 #endif // wxUSE_LONGLONG/!wxUSE_LONGLONG