SPH
Stats.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "bench/Common.h"
10 
12 
13 class Stats {
14 private:
15  Float sum = 0._f;
16  Float sumSqr = 0._f;
17  Size cnt = 0;
18  Interval range;
19 
20 public:
21  INLINE void add(const Float value) {
22  sum += value;
23  sumSqr += sqr(value);
24  range.extend(value);
25  cnt++;
26  }
27 
28  INLINE Float mean() const {
29  SPH_ASSERT(cnt != 0);
30  return sum / cnt;
31  }
32 
33  INLINE Float variance() const {
34  if (cnt < 2) {
35  return INFTY;
36  }
37  const Float cntInv = 1._f / cnt;
38  return cntInv * (sumSqr * cntInv - sqr(sum * cntInv));
39  }
40 
41  INLINE Size count() const {
42  return cnt;
43  }
44 
45  INLINE Float min() const {
46  return range.lower();
47  }
48 
49  INLINE Float max() const {
50  return range.upper();
51  }
52 };
53 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Object representing interval of real values.
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr Float INFTY
Definition: MathUtils.h:38
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_BENCHMARK_BEGIN
Definition: Common.h:7
#define NAMESPACE_BENCHMARK_END
Definition: Common.h:8
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE Float lower() const
Returns lower bound of the interval.
Definition: Interval.h:74
INLINE void extend(const Float &value)
Extends the interval to contain given value.
Definition: Interval.h:41
INLINE Float upper() const
Returns upper bound of the interval.
Definition: Interval.h:79
Definition: Stats.h:13
INLINE Float max() const
Definition: Stats.h:49
INLINE Size count() const
Definition: Stats.h:41
INLINE void add(const Float value)
Definition: Stats.h:21
INLINE Float mean() const
Definition: Stats.h:28
INLINE Float variance() const
Definition: Stats.h:33
INLINE Float min() const
Definition: Stats.h:45