tiny-profiler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 #include <chrono>
5 #include <iostream>
6 #include <cstring> // strlen
7 
9 {
10 public:
11  void tocout(long a)
12  {
13  long c = 1;
14 
15  if (a<0) { a *= -1; std::cout << "-"; }
16  while ((c *= 1000)<a);
17  while (c>1)
18  {
19  int t = (a%c) / (c / 1000);
20  std::cout << (((c>a) || (t>99)) ? "" : ((t>9) ? "0" : "00")) << t;
21  std::cout << (((c /= 1000) == 1) ? "" : ",");
22  }
23  }
24 
25  struct profiler
26  {
27  std::unordered_map<const char*, double> duration;
28  std::unordered_map<const char*, int> counts;
29  std::unordered_map<const char*,
30  std::chrono::high_resolution_clock::time_point> lasts;
31  int scope = 0;
32 
33  static profiler& instance()
34  {
35  static profiler inst;
36  return inst;
37  }
38  };
39 
40  scoped_timer(const char* key) : key(key)
41  {
43 
44  auto& lasts = profiler::instance().lasts;
45 
46  if (lasts.find(key) == lasts.end())
48 
49  auto since_last = _started - lasts[key];
50  auto sec = std::chrono::duration_cast<std::chrono::seconds>(since_last).count();
51 
52  if (sec >= 2)
53  {
55  {
56  for (int i = 0; i < 50; i++) std::cout << "=";
57  std::cout << "\n";
58  }
59  lasts[key] = _started;
60  for (int i = 0; i < profiler::instance().scope; i++)
61  std::cout << " ";
62  auto l = strlen(key);
63  std::cout << key;
64  std::cout << " ";
65  for (int i = 0; i < 50 - int(l) - profiler::instance().scope * 2; i++)
66  std::cout << ".";
67  auto avg = (profiler::instance().duration[key]
69  std::cout << " ";
70  tocout(long(avg));
71  std::cout << " usec,\t" << (profiler::instance().counts[key] / 2) << " Hz\n";
74  }
75 
77  }
78 
80  {
82  auto duration = ended - _started;
83  auto usec = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
84  profiler::instance().duration[key] += usec;
87  }
88 
89 private:
90  std::chrono::high_resolution_clock::time_point _started;
91  const char* key;
92 };
std::unordered_map< const char *, int > counts
Definition: tiny-profiler.h:28
static profiler & instance()
Definition: tiny-profiler.h:33
std::chrono::high_resolution_clock::time_point _started
Definition: tiny-profiler.h:90
std::unordered_map< const char *, std::chrono::high_resolution_clock::time_point > lasts
Definition: tiny-profiler.h:30
void tocout(long a)
Definition: tiny-profiler.h:11
GLdouble t
GLboolean GLboolean GLboolean GLboolean a
GLuint64 key
Definition: glext.h:8966
std::ostream & cout()
const GLubyte * c
Definition: glext.h:12690
const char * key
Definition: tiny-profiler.h:91
scoped_timer(const char *key)
Definition: tiny-profiler.h:40
GLint GLsizei count
int i
std::unordered_map< const char *, double > duration
Definition: tiny-profiler.h:27


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11