0027350: Support for Universal Windows Platform
[occt.git] / src / OSD / OSD_Timer.cxx
CommitLineData
b311480e 1// Created on: 1992-12-04
2// Created by: Didier PIFFAULT , Mireille MERCIEN
3// Copyright (c) 1992-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
7fd59977 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
7fd59977 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.
7fd59977 21
42cf5bc1 22#include <OSD_Timer.hxx>
7fd59977 23
57c28b61 24#ifndef _WIN32
7fd59977 25
26//---------- No Windows NT Systems ----------------------------------
27
03155c18 28#include <sys/time.h>
7fd59977 29
30//=======================================================================
31//function : GetWallClockTime
32//purpose : Get current time in seconds with system-defined precision
33//=======================================================================
34
35static 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
7fd59977 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
55static 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 :
742cc8b0 64#ifndef OCCT_UWP
999c0ca1 65 0.001 * GetTickCount();
742cc8b0 66#else
67 0.001 * GetTickCount64();
68#endif
7fd59977 69}
70
57c28b61 71#endif /* _WIN32 */
7fd59977 72
73// ====================== PLATFORM-INDEPENDENT PART ========================
74
75//=======================================================================
76//function : OSD_Timer
77//purpose :
78//=======================================================================
79
80OSD_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
94static 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
110void OSD_Timer::Reset ()
111{
112 TimeStart = TimeCumul = 0.;
113 OSD_Chronometer::Reset();
114}
115
116//=======================================================================
117//function : Show
118//purpose :
119//=======================================================================
120
7e7bbb3a 121void OSD_Timer::Show() const
7fd59977 122{
123 Show (cout);
124}
125
208e6839 126//=======================================================================
127//function : ElapsedTime
128//purpose :
129//=======================================================================
130
7e7bbb3a 131Standard_Real OSD_Timer::ElapsedTime() const
208e6839 132{
7e7bbb3a 133 if (Stopped)
208e6839 134 {
7e7bbb3a 135 return TimeCumul;
208e6839 136 }
137
7e7bbb3a 138 return TimeCumul + GetWallClockTime() - TimeStart;
208e6839 139}
7fd59977 140
141//=======================================================================
142//function : Show
143//purpose :
144//=======================================================================
145
7e7bbb3a 146void OSD_Timer::Show (Standard_Real& theSeconds,
147 Standard_Integer& theMinutes,
148 Standard_Integer& theHours,
149 Standard_Real& theCPUtime) const
7fd59977 150{
7e7bbb3a 151 const Standard_Real aTimeCumul = Stopped
152 ? TimeCumul
153 : TimeCumul + GetWallClockTime() - TimeStart;
154 Compute (aTimeCumul, theHours, theMinutes, theSeconds);
155 OSD_Chronometer::Show (theCPUtime);
7fd59977 156}
157
7fd59977 158//=======================================================================
159//function : Show
160//purpose :
161//=======================================================================
162
7e7bbb3a 163void OSD_Timer::Show (Standard_OStream& os) const
7fd59977 164{
7e7bbb3a 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;
7fd59977 177 OSD_Chronometer::Show(os);
178 os.precision (prec);
7fd59977 179}
180
181//=======================================================================
182//function : Stop
183//purpose :
184//=======================================================================
185
186void 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
200void 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}