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 // Update: J.P. TIRAULT Sep,1993
18 // On heterogeneous platforms we need to use the
19 // system call gettimeofday. This function is portable and give us
20 // elapsed time in seconds and microseconds.
22 #include <OSD_Timer.hxx>
26 //---------- No Windows NT Systems ----------------------------------
30 //=======================================================================
31 //function : GetWallClockTime
32 //purpose : Get current time in seconds with system-defined precision
33 //=======================================================================
35 static inline Standard_Real GetWallClockTime ()
38 // use time of first call as base for computing total time,
39 // to avoid loss of precision due to big values of tv_sec (counted since 1970)
40 static time_t startSec = (gettimeofday (&tv, NULL) ? 0 : tv.tv_sec);
41 return gettimeofday (&tv, NULL) ? 0. : (tv.tv_sec - startSec) + 0.000001 * tv.tv_usec;
45 //------------------- Windows NT ------------------
49 //=======================================================================
50 //function : GetWallClockTime
51 //purpose : Get current time in seconds with system-defined precision
52 //=======================================================================
54 static inline Standard_Real GetWallClockTime ()
56 // compute clock frequence on first call
57 static LARGE_INTEGER freq;
58 static BOOL isOk = QueryPerformanceFrequency (&freq);
61 return isOk && QueryPerformanceCounter (&time) ?
62 (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
64 0.001 * GetTickCount();
66 0.001 * GetTickCount64();
72 // ====================== PLATFORM-INDEPENDENT PART ========================
74 //=======================================================================
75 //function : OSD_Timer
77 //=======================================================================
79 OSD_Timer::OSD_Timer()
81 TimeStart = TimeCumul = 0.;
84 //=======================================================================
86 //purpose : Calcul les Heures,Minutes,Secondes,Millisecondes a partir
87 // de deux variables input qui sont:
88 // TimeCumulInt : Contient un periode de temps exprimee en secondes,
89 // MicroCumulInt: Contient cette meme periode exprimee en
91 //=======================================================================
93 static void Compute (Standard_Real Time,
94 Standard_Integer& heure,
95 Standard_Integer& minut,
96 Standard_Real& second)
98 Standard_Integer sec = (Standard_Integer)Time;
100 minut = (sec - heure * 3600) / 60;
101 second = Time - heure * 3600 - minut * 60;
104 //=======================================================================
107 //=======================================================================
109 void OSD_Timer::Reset (const Standard_Real theTimeElapsedSec)
112 TimeCumul = theTimeElapsedSec;
113 OSD_Chronometer::Reset();
116 //=======================================================================
119 //=======================================================================
121 void OSD_Timer::Reset ()
123 TimeStart = TimeCumul = 0.;
124 OSD_Chronometer::Reset();
127 //=======================================================================
130 //=======================================================================
132 void OSD_Timer::Restart ()
134 TimeStart = GetWallClockTime();
136 OSD_Chronometer::Restart();
139 //=======================================================================
142 //=======================================================================
144 void OSD_Timer::Show() const
149 //=======================================================================
150 //function : ElapsedTime
152 //=======================================================================
154 Standard_Real OSD_Timer::ElapsedTime() const
161 return TimeCumul + GetWallClockTime() - TimeStart;
164 //=======================================================================
167 //=======================================================================
169 void OSD_Timer::Show (Standard_Real& theSeconds,
170 Standard_Integer& theMinutes,
171 Standard_Integer& theHours,
172 Standard_Real& theCPUtime) const
174 const Standard_Real aTimeCumul = Stopped
176 : TimeCumul + GetWallClockTime() - TimeStart;
177 Compute (aTimeCumul, theHours, theMinutes, theSeconds);
178 OSD_Chronometer::Show (theCPUtime);
181 //=======================================================================
184 //=======================================================================
186 void OSD_Timer::Show (Standard_OStream& os) const
188 const Standard_Real aTimeCumul = Stopped
190 : TimeCumul + GetWallClockTime() - TimeStart;
192 Standard_Integer anHours, aMinutes;
193 Standard_Real aSeconds;
194 Compute (aTimeCumul, anHours, aMinutes, aSeconds);
196 std::streamsize prec = os.precision (12);
197 os << "Elapsed time: " << anHours << " Hours " <<
198 aMinutes << " Minutes " <<
199 aSeconds << " Seconds " << endl;
200 OSD_Chronometer::Show(os);
204 //=======================================================================
207 //=======================================================================
209 void OSD_Timer::Stop ()
212 TimeCumul += GetWallClockTime () - TimeStart;
213 OSD_Chronometer::Stop();
215 // else cout << "WARNING: OSD_Timer already Stopped !\n";
218 //=======================================================================
221 //=======================================================================
223 void OSD_Timer::Start()
226 TimeStart = GetWallClockTime();
227 OSD_Chronometer::Start();
229 // else cout << "WARNING: OSD_Timer already Running !\n";