Beatmup
profiler.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <cstdint>
21 #include <string>
22 #include <chrono>
23 #include <map>
24 #include <vector>
25 #include <ostream>
26 
27 namespace Beatmup {
28  /**
29  Collects running time statistics of multiple tracks
30  */
31  class Profiler {
32  private:
33  typedef uint64_t time_t;
34  class Track {
35  public:
37  uint32_t n;
38  Track() : n(0) {}
39  };
40 
41  std::map<std::string, Track> tracks;
42  std::vector<std::pair<std::string, std::chrono::system_clock::time_point>> running;
44 
45  public:
46  enum class ReportType {
47  BRIEF,
48  FULL
49  };
50  Profiler();
51  void reset();
52 
53  void operator ()(const std::string& track);
54  void lap();
55  void report(std::ostream&, ReportType type = ReportType::FULL) const;
56 
57  inline time_t getTotal() const { return total; }
58  };
59 
60  /**
61  Computes moving average over a given number of samples
62  */
63  template<const size_t log_length, typename datatype=float>
64  class MovingAverage {
65  private:
66  static const size_t MASK = (1 << log_length) - 1;
67  datatype samples[1 << log_length], sum;
68  size_t idx, level;
69  public:
70  inline MovingAverage(): sum(0), idx(0), level(0) {}
71 
72  inline datatype update(datatype newSample) {
73  sum += newSample;
74  if (level <= MASK)
75  ++level;
76  else
77  sum -= samples[idx & MASK];
78  samples[idx & MASK] = newSample;
79  ++idx;
80  return sum / level;
81  }
82 
83  inline datatype operator()() const {
84  return level > 0 ? sum / level : 0;
85  }
86  };
87 }
Computes moving average over a given number of samples.
Definition: profiler.h:64
datatype operator()() const
Definition: profiler.h:83
static const size_t MASK
Definition: profiler.h:66
datatype samples[1<< log_length]
Definition: profiler.h:67
datatype update(datatype newSample)
Definition: profiler.h:72
Collects running time statistics of multiple tracks.
Definition: profiler.h:31
std::map< std::string, Track > tracks
Definition: profiler.h:41
void operator()(const std::string &track)
Definition: profiler.cpp:33
uint64_t time_t
Definition: profiler.h:33
void report(std::ostream &, ReportType type=ReportType::FULL) const
Definition: profiler.cpp:56
time_t getTotal() const
Definition: profiler.h:57
std::vector< std::pair< std::string, std::chrono::system_clock::time_point > > running
Definition: profiler.h:42