1 // Created on: 1992-12-04
2 // Created by: Didier PIFFAULT , Mireille MERCIEN
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 // Update: J.P. TIRAULT Sep,1993
23 // On heterogeneous platforms we need to use the
24 // system call gettimeofday. This function is portable and give us
25 // elapsed time in seconds and microseconds.
31 #include <OSD_Timer.ixx>
35 //---------- No Windows NT Systems ----------------------------------
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
41 //=======================================================================
42 //function : GetWallClockTime
43 //purpose : Get current time in seconds with system-defined precision
44 //=======================================================================
46 static inline Standard_Real GetWallClockTime ()
49 // use time of first call as base for computing total time,
50 // to avoid loss of precision due to big values of tv_sec (counted since 1970)
51 static time_t startSec = (gettimeofday (&tv, NULL) ? 0 : tv.tv_sec);
52 return gettimeofday (&tv, NULL) ? 0. : (tv.tv_sec - startSec) + 0.000001 * tv.tv_usec;
57 //------------------- Windows NT ------------------
62 //=======================================================================
63 //function : GetWallClockTime
64 //purpose : Get current time in seconds with system-defined precision
65 //=======================================================================
67 static inline Standard_Real GetWallClockTime ()
69 // compute clock frequence on first call
70 static LARGE_INTEGER freq;
71 static BOOL isOk = QueryPerformanceFrequency (&freq);
74 return isOk && QueryPerformanceCounter (&time) ?
75 (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
76 0.000001 * GetTickCount();
81 // ====================== PLATFORM-INDEPENDENT PART ========================
83 //=======================================================================
84 //function : OSD_Timer
86 //=======================================================================
88 OSD_Timer::OSD_Timer()
90 TimeStart = TimeCumul = 0.;
93 //=======================================================================
95 //purpose : Calcul les Heures,Minutes,Secondes,Millisecondes a partir
96 // de deux variables input qui sont:
97 // TimeCumulInt : Contient un periode de temps exprimee en secondes,
98 // MicroCumulInt: Contient cette meme periode exprimee en
100 //=======================================================================
102 static void Compute (Standard_Real Time,
103 Standard_Integer& heure,
104 Standard_Integer& minut,
105 Standard_Real& second)
107 Standard_Integer sec = (Standard_Integer)Time;
109 minut = (sec - heure * 3600) / 60;
110 second = Time - heure * 3600 - minut * 60;
113 //=======================================================================
116 //=======================================================================
118 void OSD_Timer::Reset ()
120 TimeStart = TimeCumul = 0.;
121 OSD_Chronometer::Reset();
124 //=======================================================================
127 //=======================================================================
129 void OSD_Timer::Show ()
135 //=======================================================================
138 //=======================================================================
140 void OSD_Timer::Show (Standard_Real& seconds,
141 Standard_Integer& minutes,
142 Standard_Integer& hours,
143 Standard_Real& CPUtime)
145 Standard_Boolean StopSav=Stopped;
146 if (!StopSav) Stop();
148 Compute (TimeCumul, hours, minutes, seconds);
149 OSD_Chronometer::Show(CPUtime);
151 if (!StopSav) Start();
155 //=======================================================================
158 //=======================================================================
160 void OSD_Timer::Show (Standard_OStream& os)
162 Standard_Boolean StopSav=Stopped;
163 if (!StopSav) Stop();
165 Standard_Integer heure,minut;
166 Standard_Real second;
167 Compute (TimeCumul, heure, minut, second);
169 std::streamsize prec = os.precision (12);
170 os << "Elapsed time: " << heure << " Hours " <<
171 minut << " Minutes " <<
172 second << " Seconds " << endl;
173 OSD_Chronometer::Show(os);
176 if (!StopSav) Start();
179 //=======================================================================
182 //=======================================================================
184 void OSD_Timer::Stop ()
187 TimeCumul += GetWallClockTime () - TimeStart;
188 OSD_Chronometer::Stop();
190 // else cout << "WARNING: OSD_Timer already Stopped !\n";
193 //=======================================================================
196 //=======================================================================
198 void OSD_Timer::Start()
201 TimeStart = GetWallClockTime();
202 OSD_Chronometer::Start();
204 // else cout << "WARNING: OSD_Timer already Running !\n";