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"
37 #include "wx/msw/wrapwin.h"
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
62 bool CanBeUsed() const
64 return freq
.QuadPart
!= 0;
74 const int MILLISECONDS_PER_SECOND
= 1000;
75 const int MICROSECONDS_PER_MILLISECOND
= 1000;
76 const int MICROSECONDS_PER_SECOND
= 1000*1000;
78 } // anonymous namespace
80 void wxStopWatch::DoStart()
83 if ( !gs_perfCounter
.init
)
85 wxCriticalSectionLocker
lock(gs_perfCounter
.cs
);
86 ::QueryPerformanceFrequency(&gs_perfCounter
.freq
);
88 // Just a sanity check: it's not supposed to happen but verify that
89 // ::QueryPerformanceCounter() succeeds so that we can really use it.
90 LARGE_INTEGER counter
;
91 if ( !::QueryPerformanceCounter(&counter
) )
93 wxLogDebug("QueryPerformanceCounter() unexpected failed (%s), "
94 "will not use it.", wxSysErrorMsg());
96 gs_perfCounter
.freq
.QuadPart
= 0;
99 gs_perfCounter
.init
= true;
103 m_t0
= GetCurrentClockValue();
106 wxLongLong
wxStopWatch::GetClockFreq() const
109 // Under MSW we use the high resolution performance counter timer which has
110 // its own frequency (usually related to the CPU clock speed).
111 if ( gs_perfCounter
.CanBeUsed() )
112 return gs_perfCounter
.freq
.QuadPart
;
115 // Currently milliseconds are used everywhere else.
116 return MILLISECONDS_PER_SECOND
;
119 void wxStopWatch::Start(long t0
)
123 m_t0
-= (wxLongLong(t0
)*GetClockFreq())/MILLISECONDS_PER_SECOND
;
126 wxLongLong
wxStopWatch::GetCurrentClockValue() const
129 if ( gs_perfCounter
.CanBeUsed() )
131 LARGE_INTEGER counter
;
132 ::QueryPerformanceCounter(&counter
);
133 return counter
.QuadPart
;
137 return wxGetLocalTimeMillis();
140 wxLongLong
wxStopWatch::TimeInMicro() const
142 const wxLongLong
elapsed(m_pauseCount
? m_elapsedBeforePause
143 : GetCurrentClockValue() - m_t0
);
145 return (elapsed
*MICROSECONDS_PER_SECOND
)/GetClockFreq();
148 #endif // wxUSE_STOPWATCH
150 // ----------------------------------------------------------------------------
151 // old timer functions superceded by wxStopWatch
152 // ----------------------------------------------------------------------------
156 static wxLongLong wxStartTime
= 0l;
158 // starts the global timer
161 wxStartTime
= wxGetLocalTimeMillis();
164 // Returns elapsed time in milliseconds
165 long wxGetElapsedTime(bool resetTimer
)
167 wxLongLong oldTime
= wxStartTime
;
168 wxLongLong newTime
= wxGetLocalTimeMillis();
171 wxStartTime
= newTime
;
173 return (newTime
- oldTime
).GetLo();
176 #endif // wxUSE_LONGLONG