]> Creatis software - clitk.git/blob - common/clitkTimer.cxx
UNIX -> unix
[clitk.git] / common / clitkTimer.cxx
1 #ifndef CLITKTIMER_CXX
2 #define CLITKTIMER_CXX
3
4 /**
5    =================================================
6    * @file   clitkTimer.cxx
7    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
8    * @date   18 Jul 2007 16:27:45
9    * 
10    * @brief  
11    * 
12    * 
13    =================================================*/
14
15 // #ifdef UNIX
16
17 #include "clitkTimer.h"
18
19 //====================================================================
20 /// Constructs the class 
21 clitk::Timer::Timer() { 
22   Reset(); 
23 #if defined(WIN32)
24   QueryPerformanceFrequency((LARGE_INTEGER*)&mFrequency);
25 #endif
26 }
27 //====================================================================
28
29 //====================================================================
30 void clitk::Timer::Start() {
31 #if defined(unix)
32   getrusage(RUSAGE_SELF, &mBegin);
33 #elif defined(WIN32)
34   QueryPerformanceCounter((LARGE_INTEGER*)&mBegin);
35 #endif
36   mNumberOfCall++;
37 }
38 //====================================================================
39
40 //====================================================================
41 void clitk::Timer::Stop(bool accumulate) {
42 #if defined(unix)
43   getrusage(RUSAGE_SELF, &mEnd);
44   if (accumulate) {
45     mElapsed += (mEnd.ru_utime.tv_usec - mBegin.ru_utime.tv_usec)+
46       (mEnd.ru_utime.tv_sec - mBegin.ru_utime.tv_sec)*1000000;
47   }
48 #elif defined(WIN32)
49   QueryPerformanceCounter((LARGE_INTEGER*)&mEnd);
50   if (accumulate) {
51     mElapsed += ((mEnd-mBegin)*1000000)/(long double)mFrequency;
52   }
53 #endif
54   else {
55     mNumberOfCall--;
56   }
57 }
58 //====================================================================
59
60 //====================================================================
61 void clitk::Timer::Print(std::ostream & os) const {
62   if (mNumberOfCall != 1) {
63     os << "Timer #     = " << mNumberOfCall << std::endl;
64     os << "Timer total = " << mElapsed << " usec \t" << mElapsed/1000000.0 << " sec." << mElapsed/1000000.0/60 << " min."
65        << mElapsed/1000000.0/60/60 << " hours." << std::endl;
66   }
67   long double tus = mElapsed/mNumberOfCall;
68   long double ts = tus/1000000.0;
69   long double tm = ts/60.0;
70   long double th = tm/60.0;
71   os << "Timer = " << tus << " usec\t" << ts << " sec.\t" << tm << " min.\t" << th << " hours." << std::endl;
72   // os << "\tmBegin.ru_utime.tv_sec = " << mBegin.ru_utime.tv_sec << std::endl;
73 //   os << "\tmEnd.ru_utime.tv_sec = " << mEnd.ru_utime.tv_sec << std::endl;
74 //   os << "\tmBegin.ru_utime.tv_usec = " << mBegin.ru_utime.tv_usec << std::endl;
75 //   os << "\tmEnd.ru_utime.tv_usec = " << mEnd.ru_utime.tv_usec << std::endl;  
76 }
77 //====================================================================
78
79 //====================================================================
80 void clitk::Timer::Print(std::string text, std::ostream & os) const {
81   os << text;
82   Print(os);
83 }  
84 //====================================================================
85
86 //====================================================================
87 void clitk::Timer::Reset() {
88   mNumberOfCall = 0;
89   mElapsed = 0;
90 }
91 //====================================================================
92
93 // #endif // If UNIX
94 #endif /* end #define CLITKTIMER_CXX */
95