NeoPZ
timer.h
Go to the documentation of this file.
1 #ifndef _timer_h
2 #define _timer_h
3 
4 #include <sys/resource.h>
5 #include <iostream>
6 #include <iomanip>
7 
8 using namespace std;
9 
10 class Timer {
11 
12  public:
13  Timer() : state_(uninitialized), t1_(0), t2_(0) {;}
14  Timer(double floating_ops1 ) : floating_ops(floating_ops1) {}
15  ~Timer() {;}
16 
17  void start()
18  {
19  state_ = running;
20  t1_ = systemTime();
21  }
22 
23  void stop()
24  {
25  t2_ = systemTime();
26  state_ = stopped;
27  }
28 
29  double elapsedSeconds()
30  {
31  return t2_ - t1_;
32  }
33 
34  double sec()
35  {
36  return t2_ - t1_;
37  }
38 
39  void report( char const* what ) const
40  {
41  double t = t2_ - t1_;
42  std::cout << "Time for "
43  << what
44  << " = "
45  << setw(8)
46  << t
47  << " seconds [ "
48  << setw(6)
49  << floating_ops/t/1000000
50  << " Mflops]"
51  << std::endl;
52  }
53  private:
54  Timer(Timer&) { }
55  void operator=(Timer&) { }
56 
57  double systemTime()
58  {
59  getrusage(RUSAGE_SELF, &resourceUsage_);
60  double seconds = resourceUsage_.ru_utime.tv_sec
61  + resourceUsage_.ru_stime.tv_sec;
62  double micros = resourceUsage_.ru_utime.tv_usec
63  + resourceUsage_.ru_stime.tv_usec;
64  return seconds + micros/1.0e6;
65  }
66 
67  enum { uninitialized, running, stopped } state_;
68 
69  double floating_ops;
70 
71  struct rusage resourceUsage_;
72 
73  double t1_, t2_;
74 };
75 
76 
77 #endif
78 
void report(char const *what) const
Definition: timer.h:39
Timer()
Definition: timer.h:13
Definition: timer.h:10
Timer(double floating_ops1)
Definition: timer.h:14
void stop()
Definition: timer.h:23
void start()
Definition: timer.h:17
Timer(Timer &)
Definition: timer.h:54
double elapsedSeconds()
Definition: timer.h:29
double t2_
Definition: timer.h:73
void operator=(Timer &)
Definition: timer.h:55
double floating_ops
Definition: timer.h:69
double systemTime()
Definition: timer.h:57
~Timer()
Definition: timer.h:15
double sec()
Definition: timer.h:34