Salome HOME
caa6031f404cba316ac1d7653271020a00cb00a4
[modules/smesh.git] / src / SMDS / chrono.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "chrono.hxx"
21 #include "utilities.h"
22
23 using namespace std;
24
25 cntStruct* counters::_ctrs = 0;
26 int counters::_nbChrono = 0;
27
28 counters::counters(int nb)
29 {
30   MESSAGE("counters::counters(int nb)");
31   _nbChrono = nb;
32   _ctrs = new cntStruct[_nbChrono];
33
34   for (int i = 0; i < _nbChrono; i++)
35     {
36       _ctrs[i]._ctrNames = 0;
37       _ctrs[i]._ctrLines = 0;
38       _ctrs[i]._ctrOccur = 0;
39       _ctrs[i]._ctrCumul = 0;
40     }
41
42   MESSAGE("counters::counters()");
43 }
44
45 counters::~counters()
46 {
47   stats();
48 }
49
50 void counters::stats()
51 {
52   MESSAGE("counters::stats()");
53   for (int i = 0; i < _nbChrono; i++)
54     if (_ctrs[i]._ctrOccur)
55       {
56         MESSAGE("Compteur[" << i << "]: "<< _ctrs[i]._ctrNames << "[" << _ctrs[i]._ctrLines << "]");
57         MESSAGE("  " << _ctrs[i]._ctrOccur);
58         MESSAGE("  " << _ctrs[i]._ctrCumul);
59       }
60 }
61
62 chrono::chrono(int i) :
63   _ctr(i), _run(true)
64 {
65   //MESSAGE("chrono::chrono " << _ctr << " " << _run);
66   _start = clock();
67 }
68
69 chrono::~chrono()
70 {
71   if (_run)
72     stop();
73 }
74
75 void chrono::stop()
76 {
77   //MESSAGE("chrono::stop " << _ctr << " " << _run);
78   if (_run)
79     {
80       _run = false;
81       _end = clock();
82       double elapse = double(_end - _start) / double(CLOCKS_PER_SEC);
83       counters::_ctrs[_ctr]._ctrOccur++;
84       counters::_ctrs[_ctr]._ctrCumul += elapse;
85     }
86 }