profiler.h Source File

LibRPA: profiler.h Source File
LibRPA
profiler.h
Go to the documentation of this file.
1 
5 #ifndef PROFILER_H
6 #define PROFILER_H
7 #include <vector>
8 #include <ctime>
9 #include <string>
10 #include <map>
11 
12 double cpu_time_from_clocks_diff(const std::clock_t& ct_start,
13  const std::clock_t& ct_end);
14 
16 class Profiler
17 {
18 private:
20  class Timer
21  {
22  private:
24  size_t ncalls;
26  std::clock_t clock_start;
28  double wt_start;
30  double cpu_time_accu;
32  double wall_time_accu;
34  double cpu_time_last;
36  double wall_time_last;
37 
38  // private functions
39  public:
40  Timer(): ncalls(0), clock_start(0), wt_start(0), cpu_time_accu(0), wall_time_accu(0), cpu_time_last(0), wall_time_last(0) {}
42  void start() noexcept;
44  void stop() noexcept;
45  bool is_on() const { return clock_start != 0; };
46  size_t get_ncalls() const { return ncalls; };
47  double get_cpu_time() const { return cpu_time_accu; };
48  double get_wall_time() const { return wall_time_accu; };
49  double get_cpu_time_last() const { return cpu_time_last; };
50  double get_wall_time_last() const { return wall_time_last; };
51  };
53  static std::map<std::string, Timer> sd_map_timer;
55  static std::map<std::string, int> sd_map_level;
57  static std::map<std::string, std::string> sd_map_note;
59  static std::vector<std::string> sd_order;
60 
61 public:
62  Profiler() = delete;
63  ~Profiler() = delete;
64  Profiler(const Profiler&) = delete;
65  Profiler(Profiler&&) = delete;
66 
68  static void add(const char *tname, const char *tnote = "", int level = -1) noexcept;
70  static void start(const char *tname, const char *tnote = "", int level = -1) noexcept;
72  static void stop(const char *tname) noexcept;
74  static double get_cpu_time_last(const char *tname) noexcept;
76  static double get_wall_time_last(const char *tname) noexcept;
78  static void display(int verbose = 0) noexcept;
80  static int get_num_timers() noexcept { return sd_order.size(); };
81 };
82 
83 #endif
A simple profiler object to record timing of code snippet runs in the program.
Definition: profiler.h:17
static double get_wall_time_last(const char *tname) noexcept
Get wall time of last call of timer.
Definition: profiler.cpp:103
static double get_cpu_time_last(const char *tname) noexcept
Get cpu time of last call of timer.
Definition: profiler.cpp:95
static void start(const char *tname, const char *tnote="", int level=-1) noexcept
Start a timer. If the timer is not added before, add it.
Definition: profiler.cpp:72
static void stop(const char *tname) noexcept
Stop a timer and record the timing.
Definition: profiler.cpp:82
static void add(const char *tname, const char *tnote="", int level=-1) noexcept
Add a timer.
Definition: profiler.cpp:44
static void display(int verbose=0) noexcept
Display the current profiling result.
Definition: profiler.cpp:118
static int get_num_timers() noexcept
Get the number of created timers.
Definition: profiler.h:80