Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
hdr
Utility.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2021 Reactive Markets Limited
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
// http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
16
#include "
Utility.hpp
"
17
18
#include "
Histogram.hpp
"
19
#include "
Iterator.hpp
"
20
21
#include <cmath>
22
23
namespace
toolbox
{
24
inline
namespace
hdr {
25
using namespace
std
;
26
namespace
{
27
int64_t
get_count_at_percentile
(
const
Histogram& h,
double
percentile)
noexcept
28
{
29
if
(percentile > 100.0) {
30
percentile = 100.0;
31
}
32
const
int
count_at_percentile
= (percentile * h.total_count() / 100) + 0.5;
33
return
std::max<int64_t>(
count_at_percentile
, 1);
34
}
35
}
// namespace
36
37
int64_t
min
(
const
Histogram
& h)
noexcept
38
{
39
return
h.min();
40
}
41
42
int64_t
max
(
const
Histogram
& h)
noexcept
43
{
44
return
h.max();
45
}
46
47
int64_t
value_at_percentile
(
const
Histogram
& h,
double
percentile)
noexcept
48
{
49
const
int64_t
count_at_percentile
{
get_count_at_percentile
(h, percentile)};
50
51
int64_t
total
{0};
52
Iterator
iter
{h};
53
while
(
iter
.next()) {
54
total
+=
iter
.
count
();
55
if
(
total
>=
count_at_percentile
) {
56
return
h.highest_equivalent_value(
iter
.value());
57
}
58
}
59
return
0;
60
}
61
62
double
mean
(
const
Histogram
& h)
noexcept
63
{
64
const
auto
total_count = h.total_count();
65
66
int64_t
total
{0};
67
Iterator
iter
{h};
68
while
(
iter
.next()) {
69
if
(
iter
.count() != 0) {
70
total
+=
iter
.
count
() * h.median_equivalent_value(
iter
.value());
71
}
72
}
73
return
double
(
total
) / total_count;
74
}
75
76
double
stddev
(
const
Histogram
& h)
noexcept
77
{
78
const
int64_t
total_count{h.total_count()};
79
const
double
mean_val
{
mean
(h)};
80
81
double
geometric_dev_total
{0.0};
82
Iterator
iter
{h};
83
while
(
iter
.next()) {
84
if
(
iter
.count() != 0) {
85
const
double
dev
{h.median_equivalent_value(
iter
.value()) -
mean_val
};
86
geometric_dev_total
+= (
dev
*
dev
) *
iter
.
count
();
87
}
88
}
89
return
sqrt
(
geometric_dev_total
/ total_count);
90
}
91
92
}
// namespace hdr
93
}
// namespace toolbox
Histogram.hpp
Iterator.hpp
toolbox::Histogram
A High Dynamic Range (HDR) Histogram.
Definition
Histogram.hpp:64
toolbox::Iterator
Iterator is the base iterator for all iterator types.
Definition
Iterator.hpp:28
toolbox::hdr::Iterator::count
std::int64_t count() const noexcept
Value directly from array for the current counts_index.
Definition
Iterator.hpp:45
std
STL namespace.
toolbox::hdr::min
int64_t min(const Histogram &h) noexcept
Definition
Utility.cpp:37
toolbox::hdr::mean
double mean(const Histogram &h) noexcept
Definition
Utility.cpp:62
toolbox::hdr::value_at_percentile
int64_t value_at_percentile(const Histogram &h, double percentile) noexcept
Definition
Utility.cpp:47
toolbox::hdr::max
int64_t max(const Histogram &h) noexcept
Definition
Utility.cpp:42
toolbox::hdr::stddev
double stddev(const Histogram &h) noexcept
Definition
Utility.cpp:76
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
Utility.hpp
Generated by
1.9.8