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 ------------------
50 //=======================================================================
51 //function : GetWallClockTime
52 //purpose : Get current time in seconds with system-defined precision
53 //=======================================================================
55 static inline Standard_Real GetWallClockTime ()
57 // compute clock frequence on first call
58 static LARGE_INTEGER freq;
59 static BOOL isOk = QueryPerformanceFrequency (&freq);
62 return isOk && QueryPerformanceCounter (&time) ?
63 (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
65 0.001 * GetTickCount();
67 0.001 * GetTickCount64();
73 // ====================== PLATFORM-INDEPENDENT PART ========================
75 //=======================================================================
76 //function : OSD_Timer
78 //=======================================================================
80 OSD_Timer::OSD_Timer()
82 TimeStart = TimeCumul = 0.;
85 //=======================================================================
87 //purpose : Calcul les Heures,Minutes,Secondes,Millisecondes a partir
88 // de deux variables input qui sont:
89 // TimeCumulInt : Contient un periode de temps exprimee en secondes,
90 // MicroCumulInt: Contient cette meme periode exprimee en
92 //=======================================================================
94 static void Compute (Standard_Real Time,
95 Standard_Integer& heure,
96 Standard_Integer& minut,
97 Standard_Real& second)
99 Standard_Integer sec = (Standard_Integer)Time;
101 minut = (sec - heure * 3600) / 60;
102 second = Time - heure * 3600 - minut * 60;
105 //=======================================================================
108 //=======================================================================
110 void OSD_Timer::Reset ()
112 TimeStart = TimeCumul = 0.;
113 OSD_Chronometer::Reset();
116 //=======================================================================
119 //=======================================================================
121 void OSD_Timer::Show() const
126 //=======================================================================
127 //function : ElapsedTime
129 //=======================================================================
131 Standard_Real OSD_Timer::ElapsedTime() const
138 return TimeCumul + GetWallClockTime() - TimeStart;
141 //=======================================================================
144 //=======================================================================
146 void OSD_Timer::Show (Standard_Real& theSeconds,
147 Standard_Integer& theMinutes,
148 Standard_Integer& theHours,
149 Standard_Real& theCPUtime) const
151 const Standard_Real aTimeCumul = Stopped
153 : TimeCumul + GetWallClockTime() - TimeStart;
154 Compute (aTimeCumul, theHours, theMinutes, theSeconds);
155 OSD_Chronometer::Show (theCPUtime);
158 //=======================================================================
161 //=======================================================================
163 void OSD_Timer::Show (Standard_OStream& os) const
165 const Standard_Real aTimeCumul = Stopped
167 : TimeCumul + GetWallClockTime() - TimeStart;
169 Standard_Integer anHours, aMinutes;
170 Standard_Real aSeconds;
171 Compute (aTimeCumul, anHours, aMinutes, aSeconds);
173 std::streamsize prec = os.precision (12);
174 os << "Elapsed time: " << anHours << " Hours " <<
175 aMinutes << " Minutes " <<
176 aSeconds << " Seconds " << endl;
177 OSD_Chronometer::Show(os);
181 //=======================================================================
184 //=======================================================================
186 void OSD_Timer::Stop ()
189 TimeCumul += GetWallClockTime () - TimeStart;
190 OSD_Chronometer::Stop();
192 // else cout << "WARNING: OSD_Timer already Stopped !\n";
195 //=======================================================================
198 //=======================================================================
200 void OSD_Timer::Start()
203 TimeStart = GetWallClockTime();
204 OSD_Chronometer::Start();
206 // else cout << "WARNING: OSD_Timer already Running !\n";