Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
Timer.cpp
Go to the documentation of this file.
1 #include "Timer.h"
2 
3 #include "timing.h"
4 
5 #include <cmath>
6 #include <iomanip>
7 #include <algorithm>
8 #include <sstream>
9 using namespace std;
10 
11 
12 Timer::Timer() : start(get_time_ns()), last(start) { }
13 
14 
15 Timer::Timer(const Timer& other):
16  timings(other.timings),
17  order(other.order),
18  start(other.start),
19  last(other.last)
20 { }
21 
22 
23 Timer& Timer::operator=(const Timer& other) {
24  timings = other.timings;
25  order = other.order;
26  start = other.start;
27  last = other.last;
28  return *this;
29 }
30 
31 
33 
34 
35 void Timer::clear() {
36  order.clear();
37  timings.clear();
38  start = get_time_ns();
39  last = start;
40 }
41 
42 
44  last = get_time_ns();
45 }
46 
47 
48 void Timer::record(const string& name) {
49  timing_t now = get_time_ns();
50  timing_t elapsed = now - last;
51 
52  timing_map::iterator i = timings.find(name);
53  if (i == timings.end()) {
54  order.push_back(name); // track insertion order of unique keys
55  }
56  timings[name] += elapsed;
57 
58  last = now;
59 }
60 
61 
62 Timer& Timer::operator+=(const Timer& other) {
63  for (size_t i=0; i < other.order.size(); i++) {
64  const string& other_name = other.order[i];
65  if (timings.find(other_name) == timings.end()) {
66  order.push_back(other_name);
67  }
68  timings[other_name] += other[other_name];
69  }
70  return *this;
71 }
72 
73 
74 void Timer::write(std::ostream& out, bool print_total) const {
75  timing_t now = get_time_ns();
76  const string total("Total");
77  size_t max_len = total.length();
78 
79  for (size_t i=0; i < order.size(); i++) {
80  max_len = max(max_len, order[i].length());
81  }
82 
83  const size_t width = max_len + 2;
84  for (size_t i=0; i < order.size(); i++) {
85  ostringstream name;
86  name << order[i] << ":";
87  out << left << setw(width) << name.str() << (get(order[i]) / 1e9) << endl;
88  }
89 
90  if (print_total) out << left << setw(width) << total << ((now - start) / 1e9) << endl;
91 }
92 
93 
Timer()
Definition: Timer.cpp:12
void record(const std::string &name)
Records time since start or last call to record.
Definition: Timer.cpp:48
Definition: Timer.h:11
~Timer()
Definition: Timer.cpp:32
Timer & operator=(const Timer &other)
Definition: Timer.cpp:23
unsigned long long timing_t
Size for timings reported by get_time_ns.
Definition: timing.h:9
timing_t get_time_ns()
This is defined differently depending on compile-time timing options and on the platform we&#39;re using...
void fast_forward()
Skips ahead and sets last time to now.
Definition: Timer.cpp:43
void write(std::ostream &out=std::cout, bool print_total=false) const
Prints all timings (nicely formatted, in sec) to a file.
Definition: Timer.cpp:74
Timer & operator+=(const Timer &other)
Appends timings from another timer to those for this one.
Definition: Timer.cpp:62
void clear()
Empties out all recorded timings so far AND sets last_time to now.
Definition: Timer.cpp:35
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5