timers.h 2.35 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
#ifndef MRUTIL_TIMERS_H
#define MRUTIL_TIMERS_H
Martin Reinecke's avatar
Martin Reinecke committed
3
4
5
6
7

#include <chrono>
#include <string>
#include <map>

Martin Reinecke's avatar
Martin Reinecke committed
8
9
10
11
12
#include "mr_util/error_handling.h"

namespace mr {

namespace timers {
Martin Reinecke's avatar
Martin Reinecke committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

class SimpleTimer
  {
  private:
    using clock = std::chrono::steady_clock;
    clock::time_point starttime;

  public:
    SimpleTimer()
      : starttime(clock::now()) {}
    double operator()() const
      {
      return std::chrono::duration<double>(clock::now() - starttime).count();
      }
  };

class TimerHierarchy
  {
  private:
    using clock = std::chrono::steady_clock;
    class tstack_node
      {
      public:
        tstack_node *parent;
        double accTime;
        std::map<std::string,tstack_node> child;

        tstack_node(tstack_node *parent_)
          : parent(parent_), accTime(0.) {}

        double add_timings(const std::string &prefix,
          std::map<std::string, double> &res) const
          {
          double t_own = accTime;
          for (const auto &nd: child)
            t_own += nd.second.add_timings(prefix+":"+nd.first, res);
          res[prefix] = t_own;
          return t_own;
          }
      };

    clock::time_point last_time;
    tstack_node root;
    tstack_node *curnode;

    void adjust_time()
      {
      auto tnow = clock::now();
      curnode->accTime +=
        std::chrono::duration <double>(tnow - last_time).count();
      last_time = tnow;
      }

    void push_internal(const std::string &name)
      {
      auto it=curnode->child.find(name);
      if (it==curnode->child.end())
        {
Martin Reinecke's avatar
Martin Reinecke committed
71
        MR_assert(name.find(':') == std::string::npos, "reserved character");
Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        it = curnode->child.insert(make_pair(name,tstack_node(curnode))).first;
        }
      curnode=&(it->second);
      }

  public:
    TimerHierarchy()
      : last_time(clock::now()), root(nullptr), curnode(&root) {}
    void push(const std::string &name)
      {
      adjust_time();
      push_internal(name);
      }
    void pop()
      {
      adjust_time();
      curnode = curnode->parent;
Martin Reinecke's avatar
Martin Reinecke committed
89
      MR_assert(curnode!=nullptr, "tried to pop from empty timer stack");
Martin Reinecke's avatar
Martin Reinecke committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      }
    void poppush(const std::string &name)
      {
      pop();
      push_internal(name);
      }
    std::map<std::string, double> get_timings()
      {
      adjust_time();
      std::map<std::string, double> res;
      root.add_timings("root", res);
      return res;
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
105
106
}}

Martin Reinecke's avatar
Martin Reinecke committed
107
#endif