cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
custom_stat_output.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
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 */
20#pragma once
21
22#include <cstddef>
23#include <cstdint>
24#include <utility>
25#include <vector>
26
32
33namespace stat_bench {
34namespace stat {
35
39enum class CustomOutputAnalysisType : std::uint8_t {
42};
43
48public:
61 std::size_t samples, std::size_t warming_up_samples,
62 std::size_t iterations, CustomOutputAnalysisType analysis_type)
63 : name_(std::move(name)),
64 threads_(threads),
65 samples_(samples),
66 warming_up_samples_(warming_up_samples),
67 iterations_(iterations),
68 analysis_type_(analysis_type) {
69 if (samples <= warming_up_samples) {
70 throw StatBenchException("Invalid number of samples.");
71 }
72 data_.reserve(threads_);
73 const std::size_t used_samples = samples - warming_up_samples;
74 for (std::size_t i = 0; i < threads_; ++i) {
75 data_.emplace_back(used_samples, 0.0);
76 }
77 }
78
86 void add(std::size_t thread_index, std::size_t sample_index, double val) {
87 if (sample_index < warming_up_samples_) {
88 return;
89 }
90 data_.at(thread_index).at(sample_index - warming_up_samples_) += val;
91 }
92
99 const std::vector<std::vector<clock::Duration>>& durations) {
100 const std::size_t used_samples = samples_ - warming_up_samples_;
101 for (std::size_t i = 0; i < threads_; ++i) {
102 for (std::size_t j = 0; j < used_samples; ++j) {
103 double& val = data_.at(i).at(j);
104
105 switch (analysis_type_) {
107 val /= static_cast<double>(iterations_);
108 break;
110 val /= durations.at(i).at(j).seconds();
111 break;
112 }
113 }
114 }
115 }
116
122 [[nodiscard]] auto stat() const -> Statistics { return calc_stat(data_); }
123
129 [[nodiscard]] auto name() const noexcept -> const CustomOutputName& {
130 return name_;
131 }
132
138 [[nodiscard]] auto data() const noexcept
139 -> const std::vector<std::vector<double>>& {
140 return data_;
141 }
142
143private:
145 CustomOutputName name_;
146
148 std::vector<std::vector<double>> data_{};
149
151 std::size_t threads_;
152
154 std::size_t samples_;
155
157 std::size_t warming_up_samples_;
158
160 std::size_t iterations_;
161
163 CustomOutputAnalysisType analysis_type_;
164};
165
166} // namespace stat
167} // namespace stat_bench
Declaration of calc_stat function.
Class of names of custom outputs.
Class of exceptions in this library.
auto data() const noexcept -> const std::vector< std::vector< double > > &
Get data.
CustomStatOutput(CustomOutputName name, std::size_t threads, std::size_t samples, std::size_t warming_up_samples, std::size_t iterations, CustomOutputAnalysisType analysis_type)
Constructor.
void preprocess(const std::vector< std::vector< clock::Duration > > &durations)
Preprocess data.
auto name() const noexcept -> const CustomOutputName &
Get the name.
auto stat() const -> Statistics
Calculate statistics.
void add(std::size_t thread_index, std::size_t sample_index, double val)
Add a value.
Class to calculate statistics.
Definition statistics.h:31
Definition of CustomOutputName class.
Definition of Duration class.
Namespace of statistics.
Definition calc_stat.h:29
CustomOutputAnalysisType
Enumeration of types of analysis applied to custom outputs.
auto calc_stat(const std::vector< std::vector< clock::Duration > > &durations, std::size_t iterations) -> stat::Statistics
Calculate statistics.
Definition calc_stat.cpp:31
Namespace of stat_bench source codes.
STL namespace.
Definition of StatBenchException class.
Definition of Statistics class.