Medusa  1.1
Coordinate Free Mehless Method implementation
Timer.cpp
Go to the documentation of this file.
1 
8 #include <iomanip>
9 
10 namespace mm {
11 
12 void Timer::addCheckPoint(const std::string& label) {
13  assert_msg(std::find(labels_.begin(), labels_.end(), label) == labels_.end(),
14  "Label '%s' already exists. Use stopwatch to time repeatedly.", label);
15  labels_.push_back(label);
16  times_.push_back(std::chrono::steady_clock::now());
17 }
18 void Timer::showTimings(std::ostream& os) const {
19  if (labels_.size() < 2) {
20  os << "Not enough checkpoints.";
21  return;
22  }
23  size_t M = 0;
24  for (const auto& label : labels_) M = std::max(M, label.size());
25  for (size_t c = 1; c < labels_.size(); ++c) {
26  os << std::left
27  << std::setw(M) << labels_[c - 1] << " -- " << std::setw(M) << labels_[c]
28  << ' ' << std::setw(10) << std::scientific
29  << std::chrono::duration<double>(times_[c] - times_[c-1]).count() << " [s]"
30  << std::endl;
31  }
32  os << std::left << std::setw(2*M+5)
33  << "total time " << std::setw(10) // << std::scientific
34  << std::chrono::duration<double>(times_.back() - times_[0]).count()
35  << " [s]" << std::endl;
36 }
37 
38 void Timer::showTimings(const std::string& from, const std::string& to,
39  std::ostream& os) const {
40  showTimings(getID(from), getID(to), os);
41 }
42 
43 void Timer::showTimings(int from, int to, std::ostream& os) const {
44  assert_msg(0 <= from && from < size(), "From ID %d out of range [0, %d).", from, size());
45  assert_msg(0 <= to && to < size(), "To ID %d out of range [0, %d).", to, size());
46  os << std::left << std::setw(20) << labels_[from] << " -- "
47  << std::setw(20) << labels_[to] << std::setw(18)
48  << std::chrono::duration<double>(times_[to] - times_[from]).count()
49  << "[s]" << std::endl;
50 }
51 
53  assert_msg(0 <= id && id < size(), "ID %d out of range [0, %d).", id, size());
54  return times_[id];
55 }
56 
57 Timer::time_type Timer::timeAt(const std::string& label) const {
58  return times_[getID(label)];
59 }
60 double Timer::duration(const std::string& from, const std::string& to) const {
61  return std::chrono::duration<double>(times_[getID(to)] - times_[getID(from)]).count();
62 }
63 double Timer::durationToNow(const std::string& from) const {
64  return std::chrono::duration<double>(
65  std::chrono::steady_clock::now() - times_[getID(from)]).count();
66 }
67 int Timer::getID(const std::string& label) const {
68  auto it = std::find(labels_.begin(), labels_.end(), label);
69  assert_msg(it != labels_.end(), "Label '%s' not found. Available labels: %s.",
70  label.c_str(), labels_);
71  return it - labels_.begin();
72 }
73 void Timer::clear() {
74  times_.clear();
75  labels_.clear();
76 }
77 
78 } // namespace mm
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::Timer::durationToNow
double durationToNow(const std::string &from) const
Return time difference in seconds between from and now.
Definition: Timer.cpp:63
mm::Timer::getID
int getID(const std::string &label) const
Returns the ID of checkpoints with a given label.
Definition: Timer.cpp:67
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::Timer::addCheckPoint
void addCheckPoint(const std::string &label)
Adds a checkpoint with given label and remembers the time at which it was added.
Definition: Timer.cpp:12
mm::Timer::time_type
std::chrono::steady_clock::time_point time_type
Time type.
Definition: Timer.hpp:31
mm::Timer::timeAt
time_type timeAt(int id) const
Return absolute time for a given id.
Definition: Timer.cpp:52
assert.hpp
mm::Timer::times_
std::vector< time_type > times_
List of checkpoint times.
Definition: Timer.hpp:33
mm::Timer::labels_
std::vector< std::string > labels_
List of checkpoint labels.
Definition: Timer.hpp:32
mm::Timer::size
int size() const
Return the number of measurements taken.
Definition: Timer.hpp:63
Timer.hpp
mm::Timer::showTimings
void showTimings(int from, int to, std::ostream &os=std::cout) const
Output timings between the checkpoints with given ids to os.
Definition: Timer.cpp:43
mm::Timer::clear
void clear()
Clear all of time points.
Definition: Timer.cpp:73
mm::Timer::duration
double duration(const std::string &from, const std::string &to) const
Return time difference in seconds between two checkpoints.
Definition: Timer.cpp:60