NeoPZ
TPZTimer.cpp
Go to the documentation of this file.
1 
6 #include <sstream>
7 #include <algorithm>
8 
9 #include "TPZTimer.h"
10 
12 #define DIGITS2( STREAM, TIME ) \
13 if( TIME == 0 ) \
14 { \
15 STREAM << "00"; \
16 } \
17 else \
18 { \
19 if( ( TIME > 0 ) && ( TIME < 10 ) ) \
20 { \
21 STREAM << "0"; \
22 } \
23 \
24 STREAM << TIME; \
25 }
26 
27 // Starts the timer.
29 {
30  clock_t value = clock( );
31  resources.start = value;
32 
33 }
34 
35 // Stops the timer and accumulate.
37 {
38  clock_t value;
39 
40  value = clock( );
41 
42  resources.elapsed = value - resources.start;
43 
44  AccumSec = ((double) resources.elapsed)/CLOCKS_PER_SEC;
45 }
46 
47 // Prints the time nicely formated.
48 std::ostream& operator<<( std::ostream& Out, const TPZTimer& t )
49 {
50  double AcS = t.AccumSec;
51 
52  int elapsedHours = static_cast< int >( AcS / 3600.0 );
53  AcS -= static_cast< double >( elapsedHours * 3600 );
54 
55  int elapsedMins = static_cast< int >( AcS / 60.0 );
56  AcS -= static_cast< double >( elapsedMins * 60 );
57 
58  int elapsedSecs = static_cast< int >( AcS );
59  AcS -= static_cast< double >( elapsedSecs );
60 
61  double elapsedMSecs = AcS * 1000.0;
62  std::ostringstream o;
63 
64  DIGITS2( o, elapsedHours );
65  o << ":";
66 
67  DIGITS2( o, elapsedMins );
68  o << ":";
69 
70  DIGITS2( o, elapsedSecs );
71  o << " :: " << elapsedMSecs;
72 
73  Out << o.str();
74  return Out;
75 }
76 
77 // Prints the time nicely formated.
78 std::ostream& operator<<( std::ostream& Out, const TPZMultiTimer& t )
79 {
80  // First, we find the bigger process name, to align things.
81  int MaxNameSize = 0;
82 
83  for( int ii = 0; ii < t.nTimers(); ii++ )
84  {
85  MaxNameSize = std::max( MaxNameSize,
86  static_cast< int >( t.processName( ii ).size() ) );
87  }
88 
89  int Temp;
90 
91  for( int ii = 0; ii < t.nTimers(); ii++ )
92  {
93  Temp = MaxNameSize - static_cast< int >( t.processName( ii ).size() ) + 1;
94 
95  Out << t.processName( ii ) << std::string( Temp, ' ' )
96  << ": " << t.getTimer( ii ) << std::endl;
97  }
98 
99  return Out;
100 }
The timer class. Utility.
Definition: TPZTimer.h:46
Timing class. Absolutely copied from GNU time. Take a look at
std::string & processName(int i)
Gets the process name (for reporting purposes).
Definition: TPZTimer.h:224
PZResourceUsage resources
Information on the resources used.
Definition: TPZTimer.h:85
#define DIGITS2(STREAM, TIME)
Define to print two digits (increment zeros)
Definition: TPZTimer.cpp:12
void start()
Turns the timer on.
Definition: TPZTimer.cpp:28
int nTimers() const
Number of active timers.
Definition: TPZTimer.h:206
clock_t start
Wallclock time of process (start)
Definition: TPZTimer.h:28
double AccumSec
Total accumulated time in seconds.
Definition: TPZTimer.h:88
Controls several timers at once. Utility.
Definition: TPZTimer.h:100
clock_t elapsed
Wallclock time of process (elapsed)
Definition: TPZTimer.h:30
void stop()
Turns the timer off, and computes the elapsed time.
Definition: TPZTimer.cpp:36
TPZTimer & getTimer(int i)
Returns a specific timer.
Definition: TPZTimer.h:212
friend std::ostream & operator<<(std::ostream &Out, const TPZTimer &t)
Prints the time nicely formated.
Definition: TPZTimer.cpp:48