0023024: Update headers of OCCT files
[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-2012 OPEN CASCADE SAS
5 //
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.
10 //
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.
13 //
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.
20
21
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.
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <OSD_Timer.ixx>
32
33 #ifndef WNT
34
35 //---------- No Windows NT Systems ----------------------------------
36
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
39 #endif
40
41 //=======================================================================
42 //function : GetWallClockTime
43 //purpose  : Get current time in seconds with system-defined precision
44 //=======================================================================
45
46 static inline Standard_Real GetWallClockTime ()
47 {
48   struct timeval tv;
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;
53 }
54
55 #else
56
57 //-------------------  Windows NT  ------------------
58
59 #define STRICT
60 #include <windows.h>
61
62 //=======================================================================
63 //function : GetWallClockTime
64 //purpose  : Get current time in seconds with system-defined precision
65 //=======================================================================
66
67 static inline Standard_Real GetWallClockTime ()
68 {
69   // compute clock frequence on first call
70   static LARGE_INTEGER freq;
71   static BOOL isOk = QueryPerformanceFrequency (&freq);
72
73   LARGE_INTEGER time;
74   return isOk && QueryPerformanceCounter (&time) ? 
75          (Standard_Real)time.QuadPart / (Standard_Real)freq.QuadPart :
76          0.000001 * GetTickCount();
77 }
78
79 #endif /* WNT */
80
81 // ====================== PLATFORM-INDEPENDENT PART ========================
82
83 //=======================================================================
84 //function : OSD_Timer
85 //purpose  : 
86 //=======================================================================
87
88 OSD_Timer::OSD_Timer()
89 {
90   TimeStart = TimeCumul = 0.;
91 }
92
93 //=======================================================================
94 //function : Compute
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 
99 //                       microsecondes.
100 //=======================================================================
101
102 static void Compute (Standard_Real     Time,
103                      Standard_Integer& heure,
104                      Standard_Integer& minut,
105                      Standard_Real&    second) 
106 {
107   Standard_Integer sec = (Standard_Integer)Time;
108   heure = sec / 3600;
109   minut = (sec - heure * 3600) / 60;
110   second = Time - heure * 3600 - minut * 60;
111 }
112
113 //=======================================================================
114 //function : Reset
115 //purpose  : 
116 //=======================================================================
117
118 void OSD_Timer::Reset ()
119 {
120   TimeStart = TimeCumul = 0.;
121   OSD_Chronometer::Reset();
122 }
123
124 //=======================================================================
125 //function : Show
126 //purpose  : 
127 //=======================================================================
128
129 void OSD_Timer::Show ()
130 {
131   Show (cout);
132 }
133
134
135 //=======================================================================
136 //function : Show
137 //purpose  : 
138 //=======================================================================
139
140 void OSD_Timer::Show (Standard_Real&    seconds,
141                       Standard_Integer& minutes,
142                       Standard_Integer& hours,
143                       Standard_Real&    CPUtime) 
144 {
145   Standard_Boolean StopSav=Stopped;
146   if (!StopSav) Stop();
147
148   Compute (TimeCumul, hours, minutes, seconds);
149   OSD_Chronometer::Show(CPUtime);
150
151   if (!StopSav) Start();
152 }
153
154
155 //=======================================================================
156 //function : Show
157 //purpose  : 
158 //=======================================================================
159
160 void OSD_Timer::Show (Standard_OStream& os)
161 {
162   Standard_Boolean StopSav=Stopped;
163   if (!StopSav) Stop();
164   
165   Standard_Integer heure,minut;
166   Standard_Real    second;
167   Compute (TimeCumul, heure, minut, second);
168
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);
174   os.precision (prec);
175   
176   if (!StopSav) Start();  
177 }
178
179 //=======================================================================
180 //function : Stop
181 //purpose  : 
182 //=======================================================================
183
184 void OSD_Timer::Stop ()
185 {
186   if (!Stopped) {
187     TimeCumul += GetWallClockTime () - TimeStart;
188     OSD_Chronometer::Stop();
189   }
190 //  else cout << "WARNING: OSD_Timer already Stopped !\n";
191 }
192
193 //=======================================================================
194 //function : Start
195 //purpose  : 
196 //=======================================================================
197
198 void OSD_Timer::Start()
199 {
200   if (Stopped) {
201     TimeStart = GetWallClockTime();
202     OSD_Chronometer::Start();
203   }
204 //  else cout << "WARNING: OSD_Timer already Running !\n";
205 }