timer.hpp 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include <cassert>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include "easy_format.hpp"

inline std::string doubleToStringR2(double value) {
  std::stringstream stream;
  stream << std::fixed << std::setprecision(2) << value;
  return stream.str();
}

class Timer {
 public:
  std::string name;
  bool tmp_timer = false;

  Timer() {}
  Timer(std::string name) : name(name), tmp_timer(true) { start(); }
  ~Timer() {
    if (tmp_timer) {
      std::cout << name << " " << elapsedMs() << " ms" << std::endl;
    }
  }

  void start() {
    m_startTime = std::chrono::high_resolution_clock::now();
    assert(m_isRunning == false);
    m_isRunning = true;
  }

  void stop() {
    m_endTime = std::chrono::high_resolution_clock::now();
    assert(m_isRunning == true);
    m_isRunning = false;
    m_runningNs += elapsedNs();
  }

  double elapsedNs() {
    std::chrono::time_point<std::chrono::high_resolution_clock> endTime;

    if (m_isRunning) {
      endTime = std::chrono::high_resolution_clock::now();
    } else {
      endTime = m_endTime;
    }

    return std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - m_startTime).count();
  }

  void printElapsedMilliseconds() { std::cout << elapsedNs() / 1e6 << " ms" << std::endl; }

  static std::string ns_to_string(double duration) {
    auto nano_sec = duration;
    if (nano_sec >= 1000) {
      auto mirco_sec = nano_sec / 1000.0;
      if (mirco_sec >= 1000) {
        auto milli_sec = mirco_sec / 1000.0;
        if (milli_sec >= 1000) {
          auto seconds = milli_sec / 1000.0;

          if (seconds >= 60.0) {
            auto minutes = seconds / 60.0;

            if (minutes >= 60.0) {
              auto hours = minutes / 60.0;
              return doubleToStringR2(hours) + " h";
            } else {
              return doubleToStringR2(minutes) + " min";
            }
          } else {
            return doubleToStringR2(seconds) + " sec";
          }
        } else {
          return doubleToStringR2(milli_sec) + " ms";
        }
      } else {
        return doubleToStringR2(mirco_sec) + " us";
      }
    } else {
      return doubleToStringR2(nano_sec) + " ns";
    }
  }

  double runningTimeNs() { return m_runningNs; }

  std::string runningTime() {
    auto duration = m_runningNs;
    return ns_to_string(duration);
  }

  std::string elapsedTime() { return ns_to_string(elapsedNs()); }
  double elapsedMs() { return elapsedNs() / 1e6; }
  std::string report_throughput(size_t op_cnt) {
    double ops = op_cnt / elapsedMs() * 1000;
    return readable_number(ops) + "op/s";
  }

  void merge(Timer& other) {
    assert(m_isRunning == false);
    assert(other.m_isRunning == false);
    m_runningNs += other.runningTimeNs();
  }

 private:
  std::chrono::time_point<std::chrono::high_resolution_clock> m_startTime;
  std::chrono::time_point<std::chrono::high_resolution_clock> m_endTime;
  bool m_isRunning = false;
  double m_runningNs = 0.0;
};

class Counter {
 public:
  Counter() {}

  std::map<std::string, size_t> counters;

  void inc(const char* name, size_t num) { counters[name] += num; };
  void print() {
    for (auto& p : counters) {
      std::cout << p.first << " : " << p.second << std::endl;
    }
  };
};