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