45#if defined(__cpp_lib_is_invocable) || defined(STAT_BENCH_DOCUMENTATION)
53template <
typename Func,
typename... Args>
57template <
typename Func,
typename... Args>
62#ifdef STAT_BENCH_DOCUMENTATION
71template <
typename Func,
typename... Args>
86template <
typename Func,
typename... Args>
91 func(std::forward<Args>(args)...);
105template <
typename Func,
typename... Args>
128 std::size_t samples, std::size_t warm_up_samples)
129 : num_threads_(num_threads),
130 iterations_(iterations),
132 warm_up_samples_(warm_up_samples) {
133 if (num_threads_ == 0) {
136 if (iterations_ == 0) {
138 "Number of iterations must be at least one.");
143 if (samples_ <= warm_up_samples) {
145 "Number of samples for measurement must be at least one.");
147 if (num_threads_ >= 2U) {
159 template <
typename Func>
160 [[nodiscard]]
auto measure(
const Func& func)
const
161 -> std::vector<std::vector<clock::Duration>> {
162 if (num_threads_ == 1) {
163 return std::vector<std::vector<clock::Duration>>{
164 measure_here(func, 0)};
167 std::vector<std::promise<std::vector<clock::Duration>>> promises;
168 promises.resize(num_threads_);
169 std::vector<std::future<std::vector<clock::Duration>>> futures;
170 futures.reserve(num_threads_);
171 for (
auto& promise : promises) {
172 futures.push_back(promise.get_future());
175 std::vector<std::thread> threads;
176 threads.reserve(num_threads_);
177 std::exception_ptr error;
179 for (std::size_t i = 0; i < num_threads_; ++i) {
180 threads.emplace_back([
this, &func, i, &promises]() {
182 measure_here(func, i, promises[i]);
185 }
catch (
const std::exception& e) {
187 std::cerr << e.what()
191 for (
auto& thread : threads) {
195 std::vector<std::vector<clock::Duration>> durations;
196 durations.reserve(num_threads_);
197 for (
auto& future : futures) {
198 durations.push_back(future.get());
212 template <
typename Func>
213 [[nodiscard]]
auto measure_here(
const Func& func,
214 std::size_t thread_index)
const -> std::vector<clock::Duration> {
218 std::size_t sample_index = 0;
219 for (; sample_index < warm_up_samples_; ++sample_index) {
221 for (std::size_t iteration_index = 0; iteration_index < iterations_;
224 func, thread_index, sample_index, iteration_index);
230 watch.
start(samples_);
231 for (; sample_index < samples_; ++sample_index) {
233 for (std::size_t iteration_index = 0; iteration_index < iterations_;
236 func, thread_index, sample_index, iteration_index);
252 template <
typename Func>
253 void measure_here(
const Func& func, std::size_t thread_index,
254 std::promise<std::vector<clock::Duration>>& promise)
const {
256 promise.set_value(measure_here(func, thread_index));
258 promise.set_exception(std::current_exception());
263 std::size_t num_threads_;
266 std::size_t iterations_;
269 std::size_t samples_;
272 std::size_t warm_up_samples_;
275 std::shared_ptr<util::ISyncBarrier> barrier_;
Class of exceptions in this library.
ThreadableInvoker(std::size_t num_threads, std::size_t iterations, std::size_t samples, std::size_t warm_up_samples)
Constructor.
auto measure(const Func &func) const -> std::vector< std::vector< clock::Duration > >
Measure time.
auto calc_durations() const
Calculate measured durations.
void lap()
Measure a time point for a lap.
void start(std::size_t num_laps)
Start measurement.
Definition of do_not_optimize function.
Definition of Duration class.
Definition of memory_barrier function.
Namespace of internal implementation.
std::invoke_result_t< Func, Args... > invoke_result_t
Get the result type of invoking a function with arguments.
void invoke_and_ignore_return_value(const Func &func, Args &&... args)
Invoke a function and ignore the return value.
auto create_sync_barrier(std::size_t num_waiting_threads) -> std::shared_ptr< ISyncBarrier >
Create a barrier to synchronize threads.
Namespace of stat_bench source codes.
void do_not_optimize(T &&val) noexcept
Prevent compilers to optimize away a value.
void memory_barrier() noexcept
Prevent optimizers to move memory access beyond this function call.
Definition of StatBenchException class.
Definition of StopWatch class.
Definition of SyncBarrier class.