]> Creatis software - clitk.git/blob - common/clitkTimer.cxx
Add comment to precise the functionality of the inputs
[clitk.git] / common / clitkTimer.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef CLITKTIMER_CXX
19 #define CLITKTIMER_CXX
20 /**
21    =================================================
22    * @file   clitkTimer.cxx
23    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24    * @date   18 Jul 2007 16:27:45
25    *
26    * @brief
27    *
28    *
29    =================================================*/
30
31 // #ifdef UNIX
32
33 #include "clitkTimer.h"
34
35 //====================================================================
36 /// Constructs the class
37 clitk::Timer::Timer()
38 {
39   Reset();
40 #if defined(_WIN32)
41   QueryPerformanceFrequency((LARGE_INTEGER*)&mFrequency);
42 #endif
43 }
44 //====================================================================
45
46 //====================================================================
47 void clitk::Timer::Start()
48 {
49 #if defined(unix) || defined(__APPLE__)
50   getrusage(RUSAGE_SELF, &mBegin);
51 #elif defined(_WIN32)
52   QueryPerformanceCounter((LARGE_INTEGER*)&mBegin);
53 #endif
54   mNumberOfCall++;
55 }
56 //====================================================================
57
58 //====================================================================
59 void clitk::Timer::Stop(bool accumulate)
60 {
61 #if defined(unix) || defined (__APPLE__)
62   getrusage(RUSAGE_SELF, &mEnd);
63   if (accumulate) {
64     mElapsed += (mEnd.ru_utime.tv_usec - mBegin.ru_utime.tv_usec)+
65                 (mEnd.ru_utime.tv_sec - mBegin.ru_utime.tv_sec)*1000000;
66   }
67   else
68 #elif defined(_WIN32)
69   QueryPerformanceCounter((LARGE_INTEGER*)&mEnd);
70   if (accumulate) {
71     mElapsed += ((mEnd-mBegin)*1000000)/(long double)mFrequency;
72   }
73   else
74 #endif
75   {
76     mNumberOfCall--;
77   }
78 }
79 //====================================================================
80
81 //====================================================================
82 void clitk::Timer::Print(std::ostream & os) const
83 {
84   if (mNumberOfCall != 1) {
85     os << "Timer #     = " << mNumberOfCall << std::endl;
86     os << "Timer total = " << mElapsed << " usec \t" << mElapsed/1000000.0 << " sec." << mElapsed/1000000.0/60 << " min."
87        << mElapsed/1000000.0/60/60 << " hours." << std::endl;
88   }
89   long double tus = mElapsed/mNumberOfCall;
90   long double ts = tus/1000000.0;
91   long double tm = ts/60.0;
92   long double th = tm/60.0;
93   os << "Timer = " << tus << " usec\t" << ts << " sec.\t" << tm << " min.\t" << th << " hours." << std::endl;
94   // os << "\tmBegin.ru_utime.tv_sec = " << mBegin.ru_utime.tv_sec << std::endl;
95 //   os << "\tmEnd.ru_utime.tv_sec = " << mEnd.ru_utime.tv_sec << std::endl;
96 //   os << "\tmBegin.ru_utime.tv_usec = " << mBegin.ru_utime.tv_usec << std::endl;
97 //   os << "\tmEnd.ru_utime.tv_usec = " << mEnd.ru_utime.tv_usec << std::endl;
98 }
99 //====================================================================
100
101 //====================================================================
102 void clitk::Timer::Print(std::string text, std::ostream & os) const
103 {
104   os << text.c_str();
105   Print(os);
106 }
107 //====================================================================
108
109 //====================================================================
110 void clitk::Timer::Reset()
111 {
112   mNumberOfCall = 0;
113   mElapsed = 0;
114 }
115 //====================================================================
116
117 // #endif // If UNIX
118 #endif /* end #define CLITKTIMER_CXX */