Salome HOME
PR: mergefrom_PAL_OCC_21Oct04
[modules/yacs.git] / src / Utils / Utils_Timer.cxx
1 //  SALOME Utils : general SALOME's definitions and tools
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Utils_Timer.cxx
25 //  Module : SALOME
26
27 # include "Utils_Timer.hxx"
28 # include <stream.h>
29 using namespace std;
30
31 #include "utilities.h"
32
33 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
34
35 #ifndef CLK_TCK
36 # define CLK_TCK      CLOCKS_PER_SEC
37 #endif
38
39 Utils_Timer::Utils_Timer() {
40   RefToInitialTMS = new tms;
41   RefToCurrentTMS = new tms;
42
43   RefToInitialTimeB = new timeval;
44   RefToCurrentTimeB = new timeval;
45
46   Cumul_user      = Cumul_sys = 0.;
47   Stopped         = 1;
48 }
49
50 Utils_Timer::~Utils_Timer() {
51   delete RefToInitialTMS ;
52   delete RefToCurrentTMS ;
53
54   delete RefToInitialTimeB ;
55   delete RefToCurrentTimeB ;
56 }
57
58 void Utils_Timer::Start() {
59   if (Stopped) {
60     Stopped = 0;
61     times(RefToInitialTMS);
62     gettimeofday(RefToInitialTimeB,tz);
63   }
64 }
65
66 void Utils_Timer::Stop() {
67   if (!Stopped) {
68     times(RefToCurrentTMS);
69     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
70     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
71     gettimeofday(RefToCurrentTimeB,tz);
72      
73     Cumul_user += (double) diffr_user / CLK_TCK ;
74     Cumul_sys  += (double) diffr_sys  / CLK_TCK ;
75     
76     Stopped = 1;
77   }
78 }
79
80 void Utils_Timer::Show() {
81   bool StopSav = Stopped;
82   if (!StopSav) Stop();
83   MESSAGE("CPU user time: "   << Cumul_user  << " seconds ");
84   MESSAGE("CPU system time: " << Cumul_sys   << " seconds ");
85   if (!StopSav) Start();
86 }
87
88 void Utils_Timer::Reset() {
89   Stopped     = 1;
90   Cumul_user  = Cumul_sys = 0. ;
91 }
92
93 void Utils_Timer::ShowAbsolute(){
94     unsigned long Absolute_user = (unsigned long) ((timeval*)RefToCurrentTimeB)->tv_sec ;
95     MESSAGE("Absolute time: "   << Absolute_user  << " seconds ");
96 }