Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
Timer.h
Go to the documentation of this file.
1 #ifndef TIMER_H
2 #define TIMER_H
3 
4 #include <vector>
5 #include <map>
6 #include <string>
7 #include <iostream>
8 
9 #include "timing.h"
10 
11 class Timer {
12  typedef std::map<std::string, timing_t> timing_map;
13 
14  timing_map timings; /// Map from user-supplied keys to elements
15  std::vector<std::string> order; /// Keys into element map, in insertion order
16  timing_t start; /// Time this Timer was last constructedor cleared.
17  timing_t last; /// Last time restart() or record() was called.
18 
19  /// Convenience method for getting elts out of const map.
20  timing_t get(const std::string& name) const {
21  timing_map::const_iterator i = timings.find(name);
22  return (i != timings.end()) ? i->second : 0;
23  }
24 
25 
26 public:
27  Timer();
28  Timer(const Timer& other);
29  ~Timer();
30 
31  /// Empties out all recorded timings so far AND sets last_time to now.
32  void clear();
33 
34  /// Skips ahead and sets last time to now.
35  void fast_forward();
36 
37  /// Records time since start or last call to record.
38  void record(const std::string& name);
39 
40  /// Appends timings from another timer to those for this one. Also updates
41  /// last according to that of other timer.
42  Timer& operator+=(const Timer& other);
43 
44  /// Returns when the timer was initially constructed
45  timing_t start_time() const { return start; }
46 
47  /// Prints all timings (nicely formatted, in sec) to a file.
48  void write(std::ostream& out = std::cout, bool print_total = false) const;
49 
50  /// Writes AND clears.
51  void dump(std::ostream& out = std::cout, bool print_total = false) {
52  write(out, print_total);
53  clear();
54  }
55 
56  /// Returns the i-th timing recorded (starting with 0)
57  timing_t operator[](const std::string& name) const {
58  return get(name);
59  }
60 
61  Timer& operator=(const Timer& other);
62 };
63 
64 /// Syntactic sugar; calls write on the timer and passes the ostream.
65 inline std::ostream& operator<<(std::ostream& out, const Timer& timer) {
66  timer.write(out);
67  return out;
68 }
69 
70 
71 #endif // TIMER_H
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
void dump(std::ostream &out=std::cout, bool print_total=false)
Writes AND clears.
Definition: Timer.h:51
Timer & operator=(const Timer &other)
Definition: Timer.cpp:23
timing_t operator[](const std::string &name) const
Returns the i-th timing recorded (starting with 0)
Definition: Timer.h:57
unsigned long long timing_t
Size for timings reported by get_time_ns.
Definition: timing.h:9
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
std::ostream & operator<<(std::ostream &out, const Timer &timer)
Syntactic sugar; calls write on the timer and passes the ostream.
Definition: Timer.h:65
Timer & operator+=(const Timer &other)
Appends timings from another timer to those for this one.
Definition: Timer.cpp:62
timing_t start_time() const
Returns when the timer was initially constructed.
Definition: Timer.h:45
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