93ca383c3cdc739973f6acfd445e3b46fed31f4e
[occt.git] / src / OSD / OSD_Timer.cxx
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
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
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.
21
22 #include <OSD_Timer.hxx>
23
24 #ifndef _WIN32
25
26 //---------- No Windows NT Systems ----------------------------------
27
28 #include <sys/time.h>
29
30 //=======================================================================
31 //function : GetWallClockTime
32 //purpose  : Get current time in seconds with system-defined precision
33 //=======================================================================
34
35 static inline Standard_Real GetWallClockTime ()
36 {
37   struct timeval tv;
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;
42 }
43
44 #else
45 //-------------------  Windows NT  ------------------
46
47 #define STRICT
48 #include <windows.h>
49
50 //=======================================================================
51 //function : GetWallClockTime
52 //purpose  : Get current time in seconds with system-defined precision
53 //=======================================================================
54
55 static inline Standard_Real GetWallClockTime ()
56 {
57   // compute clock frequence on first call
58   static LARGE_INTEGER freq;
59   static BOOL isOk = QueryPerformanceFrequency (&freq);
60
61   LARGE_INTEGER time;
62   return isOk && QueryPerformanceCounter (&time) ? 
63          (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
64 #ifndef OCCT_UWP
65          0.001 * GetTickCount();
66 #else
67          0.001 * GetTickCount64();
68 #endif
69 }
70
71 #endif /* _WIN32 */
72
73 // ====================== PLATFORM-INDEPENDENT PART ========================
74
75 //=======================================================================
76 //function : OSD_Timer
77 //purpose  : 
78 //=======================================================================
79
80 OSD_Timer::OSD_Timer()
81 {
82   TimeStart = TimeCumul = 0.;
83 }
84
85 //=======================================================================
86 //function : Compute
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 
91 //                       microsecondes.
92 //=======================================================================
93
94 static void Compute (Standard_Real     Time,
95                      Standard_Integer& heure,
96                      Standard_Integer& minut,
97                      Standard_Real&    second) 
98 {
99   Standard_Integer sec = (Standard_Integer)Time;
100   heure = sec / 3600;
101   minut = (sec - heure * 3600) / 60;
102   second = Time - heure * 3600 - minut * 60;
103 }
104
105 //=======================================================================
106 //function : Reset
107 //purpose  : 
108 //=======================================================================
109
110 void OSD_Timer::Reset ()
111 {
112   TimeStart = TimeCumul = 0.;
113   OSD_Chronometer::Reset();
114 }
115
116 //=======================================================================
117 //function : Show
118 //purpose  : 
119 //=======================================================================
120
121 void OSD_Timer::Show() const
122 {
123   Show (cout);
124 }
125
126 //=======================================================================
127 //function : ElapsedTime
128 //purpose  :
129 //=======================================================================
130
131 Standard_Real OSD_Timer::ElapsedTime() const
132 {
133   if (Stopped)
134   {
135     return TimeCumul;
136   }
137
138   return TimeCumul + GetWallClockTime() - TimeStart;
139 }
140
141 //=======================================================================
142 //function : Show
143 //purpose  : 
144 //=======================================================================
145
146 void OSD_Timer::Show (Standard_Real&    theSeconds,
147                       Standard_Integer& theMinutes,
148                       Standard_Integer& theHours,
149                       Standard_Real&    theCPUtime) const
150 {
151   const Standard_Real aTimeCumul = Stopped
152                                  ? TimeCumul
153                                  : TimeCumul + GetWallClockTime() - TimeStart;
154   Compute (aTimeCumul, theHours, theMinutes, theSeconds);
155   OSD_Chronometer::Show (theCPUtime);
156 }
157
158 //=======================================================================
159 //function : Show
160 //purpose  : 
161 //=======================================================================
162
163 void OSD_Timer::Show (Standard_OStream& os) const
164 {
165   const Standard_Real aTimeCumul = Stopped
166                                  ? TimeCumul
167                                  : TimeCumul + GetWallClockTime() - TimeStart;
168
169   Standard_Integer anHours, aMinutes;
170   Standard_Real    aSeconds;
171   Compute (aTimeCumul, anHours, aMinutes, aSeconds);
172
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);
178   os.precision (prec);
179 }
180
181 //=======================================================================
182 //function : Stop
183 //purpose  : 
184 //=======================================================================
185
186 void OSD_Timer::Stop ()
187 {
188   if (!Stopped) {
189     TimeCumul += GetWallClockTime () - TimeStart;
190     OSD_Chronometer::Stop();
191   }
192 //  else cout << "WARNING: OSD_Timer already Stopped !\n";
193 }
194
195 //=======================================================================
196 //function : Start
197 //purpose  : 
198 //=======================================================================
199
200 void OSD_Timer::Start()
201 {
202   if (Stopped) {
203     TimeStart = GetWallClockTime();
204     OSD_Chronometer::Start();
205   }
206 //  else cout << "WARNING: OSD_Timer already Running !\n";
207 }