Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/kernel.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 <iostream>
29 using namespace std;
30
31 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
32
33 #ifndef CLK_TCK
34 # define CLK_TCK      CLOCKS_PER_SEC
35 #endif
36
37 Utils_Timer::Utils_Timer() {
38   RefToInitialTMS = new tms;
39   RefToCurrentTMS = new tms;
40
41   RefToInitialTimeB = new timeval;
42   RefToCurrentTimeB = new timeval;
43
44   Cumul_user      = Cumul_sys = 0.;
45   Stopped         = 1;
46 }
47
48 Utils_Timer::~Utils_Timer() {
49   delete RefToInitialTMS ;
50   delete RefToCurrentTMS ;
51
52   delete RefToInitialTimeB ;
53   delete RefToCurrentTimeB ;
54 }
55
56 void Utils_Timer::Start() {
57   if (Stopped) {
58     Stopped = 0;
59     times(RefToInitialTMS);
60     gettimeofday(RefToInitialTimeB,tz);
61   }
62 }
63
64 void Utils_Timer::Stop() {
65   if (!Stopped) {
66     times(RefToCurrentTMS);
67     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
68     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
69     gettimeofday(RefToCurrentTimeB,tz);
70      
71     Cumul_user += (double) diffr_user / CLK_TCK ;
72     Cumul_sys  += (double) diffr_sys  / CLK_TCK ;
73     
74     Stopped = 1;
75   }
76 }
77
78 void Utils_Timer::Show() {
79   bool StopSav = Stopped;
80   if (!StopSav) Stop();
81   cout << "CPU user time: "   << Cumul_user  << " seconds " << endl;
82   cout << "CPU system time: " << Cumul_sys   << " seconds " << endl;
83   if (!StopSav) Start();
84 }
85
86 void Utils_Timer::Reset() {
87   Stopped     = 1;
88   Cumul_user  = Cumul_sys = 0. ;
89 }
90
91 void Utils_Timer::ShowAbsolute(){
92     unsigned long Absolute_user = (unsigned long) ((timeval*)RefToCurrentTimeB)->tv_sec ;
93     cout << "Absolute time: "   << Absolute_user  << " seconds " << endl;
94 }