NeoPZ
timing_analysis.h
Go to the documentation of this file.
1 
5 #ifndef TIMING_ANALYSIS_H
6 
7 #ifndef PERF_ANALYSIS
8 
9 #define TIME_ANALYSIS_ADD(ta, timer, ...)
10 #define TIME_SEC_BEG_LOG(logger,timer, ...)
11 #define TIME_SEC_END_LOG(logger,ta,timer, ...)
12 #define TIME_SEC_BEG(timer, ...)
13 #define TIME_SEC_END(ta,timer, ...)
14 
15 #else
16 
17 #pragma message ( "warning: PERF_ANALYSIS is defined..." )
18 /* Sanity checking */
19 
20 #ifndef NDEBUG
21 #error "PERF_ANALYSIS require the code to be compiled with NDEBUG"
22 #endif
23 #ifndef NODEBUG
24 #error "PERF_ANALYSIS require the code to be compiled with NODEBUG"
25 #endif
26 #if (defined LOG4CXX) && !(defined PERF_DEBUG)
27 #error "PERF_ANALYSIS require the code to be compiled without LOG4CXX"
28 #endif
29 
30 #include<string>
31 #include<set>
32 #include<iostream>
33 
34 #define TIME_ANALYSIS_ADD(ta, timer, ...) \
35  { std::ostringstream ostr; \
36  ostr << __VA_ARGS__; \
37  ta.add(timer.getUnits(), ostr.str()); }
38 
39 #define TIME_SEC_BEG_LOG(logger,timer, ...) \
40  LOG4CXX_INFO(logger, __VA_ARGS__ << " started") \
41  timer.start()
42 
43 #define TIME_SEC_END_LOG(logger,ta,timer, ...) \
44  timer.stop(); \
45  TIME_ANALYSIS_ADD(ta,timer, __VA_ARGS__) \
46  LOG4CXX_INFO(logger, __VA_ARGS__ << " ended in " << timer.getTime())
47 
48 #define TIME_SEC_BEG(timer, ...) \
49  timer.start()
50 
51 #define TIME_SEC_END(ta,timer, ...) \
52  timer.stop(); \
53  TIME_ANALYSIS_ADD(ta,timer, __VA_ARGS__)
54 
55 typedef std::pair<double,std::string> section_timing_t;
56 typedef std::set<section_timing_t> timing_set_t;
57 
58 class TimingAnalysis
59 {
60  public:
61  TimingAnalysis() {};
62 
63  void add(double units, const std::string& section_name) {
64  section_timing_t st(units,section_name);
65  ts.insert(st);
66  }
67 
68  void share_report(std::ostream& o, double total) {
69  double share;
70  double sum=0;
71  o << "Timing analysis Report" << std::endl;
72  timing_set_t::iterator it;
73  for(it = ts.begin(); it != ts.end(); it++) {
74  sum += it->first;
75  share = 100 * (it->first / total);
76  o << std::setw(6) << std::setprecision(2)
77  << std::fixed << share << " % : ";
78  o << it->second << " : " << it->first << std::endl;
79  }
80  share = 100 * (sum / total);
81  o << std::setw(6) << std::setprecision(2)
82  << std::fixed << share << " % : Total";
83  o << " : " << sum << std::endl;
84  }
85 
86  void share_report(std::ostream& o) {
87  double total = 0;
88  timing_set_t::iterator it;
89  for(it = ts.begin(); it != ts.end(); it++) {
90  total += it->first;
91  }
92  share_report(o, total);
93  }
94 
95  private:
96 
97  timing_set_t ts;
98 
99 };
100 
101 #endif // PERF_ANALYSIS
102 
103 #endif // TIMING_ANALYSIS_H
104