OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuTimer Class Reference

#include <CpuTimer.hpp>

Classes

struct  EventTimer
 

Public Types

enum  TimerResolution { TimerResolution::kSeconds, TimerResolution::kMilliseconds, TimerResolution::kMicroseconds, TimerResolution::kNanoseconds }
 

Public Member Functions

 CpuTimer (const std::string &label)
 
 CpuTimer (const std::string &label, const int64_t nest_level)
 
 ~CpuTimer ()
 
void start_event_timer (const std::string &event_label)
 
std::shared_ptr< CpuTimerstart_nested_event_timer (const std::string &event_label)
 
void end_event_timer ()
 

Private Types

using time_point = std::chrono::time_point< std::chrono::high_resolution_clock >
 

Private Member Functions

EventTimerstart_event_timer_impl (const std::string &event_label, const bool make_nested)
 
void initialize ()
 
void finalize ()
 
void print_timings ()
 

Private Attributes

const std::string timer_label_
 
const size_t nest_level_
 
time_point timers_start_
 
time_point timers_end_
 
std::vector< EventTimerevent_timers_
 

Detailed Description

Definition at line 30 of file CpuTimer.hpp.

Member Typedef Documentation

using CpuTimer::time_point = std::chrono::time_point<std::chrono::high_resolution_clock>
private

Definition at line 31 of file CpuTimer.hpp.

Member Enumeration Documentation

Enumerator
kSeconds 
kMilliseconds 
kMicroseconds 
kNanoseconds 

Definition at line 34 of file CpuTimer.hpp.

34 { kSeconds, kMilliseconds, kMicroseconds, kNanoseconds };

Constructor & Destructor Documentation

CpuTimer::CpuTimer ( const std::string &  label)
inline

Definition at line 36 of file CpuTimer.hpp.

References initialize().

37  initialize();
38  }
const std::string timer_label_
Definition: CpuTimer.hpp:157
const size_t nest_level_
Definition: CpuTimer.hpp:158
void initialize()
Definition: CpuTimer.hpp:106

+ Here is the call graph for this function:

CpuTimer::CpuTimer ( const std::string &  label,
const int64_t  nest_level 
)
inline

Definition at line 39 of file CpuTimer.hpp.

References initialize().

40  : timer_label_(label), nest_level_(nest_level) {
41  initialize();
42  }
const std::string timer_label_
Definition: CpuTimer.hpp:157
const size_t nest_level_
Definition: CpuTimer.hpp:158
void initialize()
Definition: CpuTimer.hpp:106

+ Here is the call graph for this function:

CpuTimer::~CpuTimer ( )
inline

Definition at line 44 of file CpuTimer.hpp.

References nest_level_, and print_timings().

44  {
45  if (nest_level_ == 0) {
46  print_timings();
47  }
48  }
const size_t nest_level_
Definition: CpuTimer.hpp:158
void print_timings()
Definition: CpuTimer.hpp:115

+ Here is the call graph for this function:

Member Function Documentation

void CpuTimer::end_event_timer ( )
inline

Definition at line 59 of file CpuTimer.hpp.

References event_timers_.

59  {
60  if (!event_timers_.empty()) {
61  event_timers_.back().finalize();
62  }
63  }
std::vector< EventTimer > event_timers_
Definition: CpuTimer.hpp:161
void CpuTimer::finalize ( )
inlineprivate

Definition at line 108 of file CpuTimer.hpp.

References event_timers_, and timers_end_.

Referenced by print_timings().

108  {
109  timers_end_ = std::chrono::high_resolution_clock::now();
110  if (!event_timers_.empty()) {
111  event_timers_.back().finalize();
112  }
113  }
time_point timers_end_
Definition: CpuTimer.hpp:160
std::vector< EventTimer > event_timers_
Definition: CpuTimer.hpp:161

+ Here is the caller graph for this function:

void CpuTimer::initialize ( )
inlineprivate

Definition at line 106 of file CpuTimer.hpp.

References timers_start_.

Referenced by CpuTimer().

106 { timers_start_ = std::chrono::high_resolution_clock::now(); }
time_point timers_start_
Definition: CpuTimer.hpp:159

+ Here is the caller graph for this function:

void CpuTimer::print_timings ( )
inlineprivate

Definition at line 115 of file CpuTimer.hpp.

References event_timers_, finalize(), report::header, nest_level_, timer_label_, timers_end_, and timers_start_.

Referenced by ~CpuTimer().

115  {
116  if (nest_level_ == 0) {
117  finalize();
118  }
119  const std::string header{timer_label_ + " Timings"};
120  const std::string header_underline{std::string(header.size(), '=')};
121  const std::string nest_spaces{std::string(nest_level_ * 2, ' ')};
122  std::cout << std::endl
123  << nest_spaces << header << std::endl
124  << nest_spaces << header_underline << std::endl;
125 
126  const size_t timings_left_margin{50};
127 
128  for (auto& event_timer : event_timers_) {
129  const int64_t ms_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
130  event_timer.end_time - event_timer.start_time)
131  .count();
132  const int64_t total_ms_elapsed =
133  std::chrono::duration_cast<std::chrono::milliseconds>(event_timer.end_time -
135  .count();
136  const size_t label_width{event_timer.event_label.size()};
137  const size_t margin_calc{
138  label_width < timings_left_margin ? timings_left_margin - label_width : 0};
139 
140  std::cout << nest_spaces << event_timer.event_label << ": "
141  << std::setw(margin_calc) << std::fixed << std::setprecision(4)
142  << ms_elapsed << " ms elapsed, " << total_ms_elapsed << " ms total"
143  << std::endl;
144 
145  if (event_timer.nested_event_timer != nullptr) {
146  event_timer.nested_event_timer->print_timings();
147  std::cout << std::endl;
148  }
149  }
150  const int64_t total_ms_elapsed =
151  std::chrono::duration_cast<std::chrono::milliseconds>(timers_end_ - timers_start_)
152  .count();
153  std::cout << nest_spaces << timer_label_ << " total elapsed: " << std::fixed
154  << std::setprecision(4) << total_ms_elapsed << " ms" << std::endl;
155  }
const std::string timer_label_
Definition: CpuTimer.hpp:157
time_point timers_start_
Definition: CpuTimer.hpp:159
const size_t nest_level_
Definition: CpuTimer.hpp:158
void finalize()
Definition: CpuTimer.hpp:108
time_point timers_end_
Definition: CpuTimer.hpp:160
std::vector< EventTimer > event_timers_
Definition: CpuTimer.hpp:161
list header
Definition: report.py:113

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CpuTimer::start_event_timer ( const std::string &  event_label)
inline

Definition at line 50 of file CpuTimer.hpp.

References start_event_timer_impl().

50  {
51  start_event_timer_impl(event_label, false);
52  }
EventTimer & start_event_timer_impl(const std::string &event_label, const bool make_nested)
Definition: CpuTimer.hpp:97

+ Here is the call graph for this function:

EventTimer& CpuTimer::start_event_timer_impl ( const std::string &  event_label,
const bool  make_nested 
)
inlineprivate

Definition at line 97 of file CpuTimer.hpp.

References event_timers_, and nest_level_.

Referenced by start_event_timer(), and start_nested_event_timer().

98  {
99  if (!event_timers_.empty()) {
100  event_timers_.back().finalize();
101  }
102  event_timers_.emplace_back(EventTimer(event_label, nest_level_, make_nested));
103  return event_timers_.back();
104  }
const size_t nest_level_
Definition: CpuTimer.hpp:158
std::vector< EventTimer > event_timers_
Definition: CpuTimer.hpp:161

+ Here is the caller graph for this function:

std::shared_ptr<CpuTimer> CpuTimer::start_nested_event_timer ( const std::string &  event_label)
inline

Definition at line 54 of file CpuTimer.hpp.

References start_event_timer_impl().

54  {
55  auto& event_timer = start_event_timer_impl(event_label, true);
56  return event_timer.nested_event_timer;
57  }
EventTimer & start_event_timer_impl(const std::string &event_label, const bool make_nested)
Definition: CpuTimer.hpp:97

+ Here is the call graph for this function:

Member Data Documentation

std::vector<EventTimer> CpuTimer::event_timers_
private

Definition at line 161 of file CpuTimer.hpp.

Referenced by end_event_timer(), finalize(), print_timings(), and start_event_timer_impl().

const size_t CpuTimer::nest_level_
private

Definition at line 158 of file CpuTimer.hpp.

Referenced by print_timings(), start_event_timer_impl(), and ~CpuTimer().

const std::string CpuTimer::timer_label_
private

Definition at line 157 of file CpuTimer.hpp.

Referenced by print_timings().

time_point CpuTimer::timers_end_
private

Definition at line 160 of file CpuTimer.hpp.

Referenced by finalize(), and print_timings().

time_point CpuTimer::timers_start_
private

Definition at line 159 of file CpuTimer.hpp.

Referenced by initialize(), and print_timings().


The documentation for this class was generated from the following file: