Version: 1.0
performance_evaluator.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_PERFORMANCE_EVALUATOR_H_
8 #define GRAPHGEN_PERFORMANCE_EVALUATOR_H_
9 
10 #include <map>
11 #include <chrono>
12 
14  using hrc = std::chrono::high_resolution_clock;
15  using tp = hrc::time_point;
16 
17  struct Elapsed {
18  double last = 0, total = 0;
19  };
20 
21  tp last_;
22 
23 public:
24  void start() {
25  last_ = hrc::now();
26  }
27  double stop() {
28  auto cur = hrc::now();
29  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(cur - last_).count();
30  double t = static_cast<double>(duration);
31  counter_.last = t;
32  counter_.total += t;
33  return counter_.last;
34  }
35 
36  void reset() { counter_.total = 0; }
37  double last() const { return counter_.last; }
38  double total() const { return counter_.total; }
39 
40  void store(const std::string& s, double time /*milliseconds*/) {
41  counters_[s].last = time;
42  counters_[s].total += time;
43  }
44  double get(const std::string& s) {
45  return counters_.at(s).last;
46  }
47  bool find(const std::string& s) {
48  return counters_.find(s) != counters_.end();
49  }
50 
51 private:
52  Elapsed counter_;
53  std::map<std::string, Elapsed> counters_;
54 };
55 
56 #endif // !GRAPHGEN_PERFORMANCE_EVALUATOR_H_
void store(const std::string &s, double time)
bool find(const std::string &s)
double get(const std::string &s)