]> Creatis software - clitk.git/blob - common/clitkTimer.cxx
89297f3088ae88f1511cd231ca2026ac0d856a6b
[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://oncora1.lyon.fnclcc.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   Reset(); 
39 #if defined(WIN32)
40   QueryPerformanceFrequency((LARGE_INTEGER*)&mFrequency);
41 #endif
42 }
43 //====================================================================
44
45 //====================================================================
46 void clitk::Timer::Start() {
47 #if defined(unix) || defined(__APPLE__)
48   getrusage(RUSAGE_SELF, &mBegin);
49 #elif defined(WIN32)
50   QueryPerformanceCounter((LARGE_INTEGER*)&mBegin);
51 #endif
52   mNumberOfCall++;
53 }
54 //====================================================================
55
56 //====================================================================
57 void clitk::Timer::Stop(bool accumulate) {
58 #if defined(unix) || defined (__APPLE__)
59   getrusage(RUSAGE_SELF, &mEnd);
60   if (accumulate) {
61     mElapsed += (mEnd.ru_utime.tv_usec - mBegin.ru_utime.tv_usec)+
62       (mEnd.ru_utime.tv_sec - mBegin.ru_utime.tv_sec)*1000000;
63   }
64 #elif defined(WIN32)
65   QueryPerformanceCounter((LARGE_INTEGER*)&mEnd);
66   if (accumulate) {
67     mElapsed += ((mEnd-mBegin)*1000000)/(long double)mFrequency;
68   }
69 #endif
70   else {
71     mNumberOfCall--;
72   }
73 }
74 //====================================================================
75
76 //====================================================================
77 void clitk::Timer::Print(std::ostream & os) const {
78   if (mNumberOfCall != 1) {
79     os << "Timer #     = " << mNumberOfCall << std::endl;
80     os << "Timer total = " << mElapsed << " usec \t" << mElapsed/1000000.0 << " sec." << mElapsed/1000000.0/60 << " min."
81        << mElapsed/1000000.0/60/60 << " hours." << std::endl;
82   }
83   long double tus = mElapsed/mNumberOfCall;
84   long double ts = tus/1000000.0;
85   long double tm = ts/60.0;
86   long double th = tm/60.0;
87   os << "Timer = " << tus << " usec\t" << ts << " sec.\t" << tm << " min.\t" << th << " hours." << std::endl;
88   // os << "\tmBegin.ru_utime.tv_sec = " << mBegin.ru_utime.tv_sec << std::endl;
89 //   os << "\tmEnd.ru_utime.tv_sec = " << mEnd.ru_utime.tv_sec << std::endl;
90 //   os << "\tmBegin.ru_utime.tv_usec = " << mBegin.ru_utime.tv_usec << std::endl;
91 //   os << "\tmEnd.ru_utime.tv_usec = " << mEnd.ru_utime.tv_usec << std::endl;  
92 }
93 //====================================================================
94
95 //====================================================================
96 void clitk::Timer::Print(std::string text, std::ostream & os) const {
97   os << text;
98   Print(os);
99 }  
100 //====================================================================
101
102 //====================================================================
103 void clitk::Timer::Reset() {
104   mNumberOfCall = 0;
105   mElapsed = 0;
106 }
107 //====================================================================
108
109 // #endif // If UNIX
110 #endif /* end #define CLITKTIMER_CXX */
111