Welcome to mirror list, hosted at ThFree Co, Russian Federation.

time_samples.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c4f1fdf072c96f1a8a3e1130ecd3cabfdab5a4bd (plain)
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
#pragma once

#include "base/timer.hpp"

#include "std/cstdint.hpp"

namespace my
{
// This class can be used in measurements of code blocks performance.
// It can accumulate time samples, and can calculate mean time and
// standard deviation.
//
// *NOTE* This class is NOT thread-safe.
class TimeSamples final
{
public:
  void Add(double seconds);

  // Mean of the accumulated time samples.
  double GetMean() const;

  // Unbiased standard deviation of the accumulated time samples.
  double GetSD() const;

  // Unbiased variance of the accumulated time samples.
  double GetVar() const;

private:
  double m_sum = 0.0;
  double m_sum2 = 0.0;
  size_t m_total = 0;
};

// This class can be used as a single scoped time sample. It
// automatically adds time sample on destruction.
//
// *NOTE* This class is NOT thread-safe.
class ScopedTimeSample final
{
public:
  ScopedTimeSample(TimeSamples & ts) : m_ts(ts) {}
  ~ScopedTimeSample() { m_ts.Add(m_t.ElapsedSeconds()); }

private:
  TimeSamples & m_ts;
  my::Timer m_t;
};

}  // namespace my