1 // Created on: 1992-12-04
2 // Created by: Didier PIFFAULT , Mireille MERCIEN
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <OSD_Timer.hxx>
23 //=======================================================================
24 //function : GetWallClockTime
25 //purpose : Get current time in seconds with system-defined precision
26 //=======================================================================
28 static inline Standard_Real GetWallClockTime ()
31 // use time of first call as base for computing total time,
32 // to avoid loss of precision due to big values of tv_sec (counted since 1970)
33 static time_t startSec = (gettimeofday (&tv, NULL) ? 0 : tv.tv_sec);
34 return gettimeofday (&tv, NULL) ? 0. : (tv.tv_sec - startSec) + 0.000001 * tv.tv_usec;
41 //=======================================================================
42 //function : GetWallClockTime
43 //purpose : Get current time in seconds with system-defined precision
44 //=======================================================================
46 static inline Standard_Real GetWallClockTime ()
48 // compute clock frequence on first call
49 static LARGE_INTEGER freq;
50 static BOOL isOk = QueryPerformanceFrequency (&freq);
53 return isOk && QueryPerformanceCounter (&time) ?
54 (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
55 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
56 0.001 * GetTickCount64();
58 0.001 * GetTickCount();
66 //! Auxiliary function splits elapsed time in seconds into Hours, Minutes and Seconds.
67 //! @param theTimeSec [in] elapsed time in seconds
68 //! @param theHours [out] clamped elapsed hours
69 //! @param theMinutes [out] clamped elapsed minutes within range [0, 59]
70 //! @param theSeconds [out] clamped elapsed seconds within range [0, 60)
71 static void timeToHoursMinutesSeconds (Standard_Real theTimeSec,
72 Standard_Integer& theHours,
73 Standard_Integer& theMinutes,
74 Standard_Real& theSeconds)
76 Standard_Integer aSec = (Standard_Integer)theTimeSec;
77 theHours = aSec / 3600;
78 theMinutes = (aSec - theHours * 3600) / 60;
79 theSeconds = theTimeSec - theHours * 3600 - theMinutes * 60;
83 //=======================================================================
84 //function : OSD_Timer
86 //=======================================================================
88 OSD_Timer::OSD_Timer (Standard_Boolean theThisThreadOnly)
89 : OSD_Chronometer (theThisThreadOnly),
96 //=======================================================================
99 //=======================================================================
101 void OSD_Timer::Reset (const Standard_Real theTimeElapsedSec)
104 myTimeCumul = theTimeElapsedSec;
105 OSD_Chronometer::Reset();
108 //=======================================================================
111 //=======================================================================
113 void OSD_Timer::Reset ()
115 myTimeStart = myTimeCumul = 0.0;
116 OSD_Chronometer::Reset();
119 //=======================================================================
122 //=======================================================================
124 void OSD_Timer::Restart ()
126 myTimeStart = GetWallClockTime();
128 OSD_Chronometer::Restart();
131 //=======================================================================
134 //=======================================================================
136 void OSD_Timer::Show() const
141 //=======================================================================
142 //function : ElapsedTime
144 //=======================================================================
146 Standard_Real OSD_Timer::ElapsedTime() const
153 return myTimeCumul + GetWallClockTime() - myTimeStart;
156 //=======================================================================
159 //=======================================================================
161 void OSD_Timer::Show (Standard_Real& theSeconds,
162 Standard_Integer& theMinutes,
163 Standard_Integer& theHours,
164 Standard_Real& theCPUtime) const
166 const Standard_Real aTimeCumul = myIsStopped
168 : myTimeCumul + GetWallClockTime() - myTimeStart;
169 timeToHoursMinutesSeconds (aTimeCumul, theHours, theMinutes, theSeconds);
170 OSD_Chronometer::Show (theCPUtime);
173 //=======================================================================
176 //=======================================================================
178 void OSD_Timer::Show (Standard_OStream& theOStream) const
180 const Standard_Real aTimeCumul = ElapsedTime();
182 Standard_Integer anHours, aMinutes;
183 Standard_Real aSeconds;
184 timeToHoursMinutesSeconds (aTimeCumul, anHours, aMinutes, aSeconds);
186 std::streamsize prec = theOStream.precision (12);
187 theOStream << "Elapsed time: " << anHours << " Hours " <<
188 aMinutes << " Minutes " <<
189 aSeconds << " Seconds\n";
190 OSD_Chronometer::Show (theOStream);
191 theOStream.precision (prec);
194 //=======================================================================
197 //=======================================================================
199 void OSD_Timer::Stop ()
203 myTimeCumul += GetWallClockTime() - myTimeStart;
204 OSD_Chronometer::Stop();
208 //=======================================================================
211 //=======================================================================
213 void OSD_Timer::Start()
217 myTimeStart = GetWallClockTime();
218 OSD_Chronometer::Start();