GNU Radio Manual and C++ API Reference  3.10.7.0
The Free & Open Software Radio Ecosystem
benchmark_common.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2004,2013,2018 Free Software Foundation, Inc.
4  * Copyright 2023 Marcus Müller
5  *
6  * This file is part of GNU Radio
7  *
8  * SPDX-License-Identifier: GPL-3.0-or-later
9  *
10  */
11 #ifndef INCLUDED_BENCHMARK_COMMON
12 #define INCLUDED_BENCHMARK_COMMON
13 #include <spdlog/fmt/fmt.h>
14 #include <string_view>
15 #include <chrono>
16 #include <cstddef>
17 #include <cstdlib>
18 #include <vector>
19 
20 template <typename functor>
21 [[nodiscard]] auto benchmark(functor test, size_t block_size)
22 {
23  using namespace std::chrono;
24  std::vector<float> outp(2 * block_size);
25  float* output = outp.data();
26  float *x = &output[0], *y = &output[block_size];
27 
28  // touch memory
29  memset(output, 0, 2 * block_size * sizeof(float));
30 
31  auto before = high_resolution_clock::now();
32  // do the actual work
33 
34  test(x, y);
35 
36  auto after = high_resolution_clock::now();
37  // get ending CPU usage
38  auto dur = duration_cast<duration<double, std::ratio<1, 1>>>(after - before);
39  return dur;
40 }
41 template <typename dur_t>
42 auto format_duration(std::string_view name,
43  dur_t dur,
44  size_t iterations,
45  size_t block_size)
46 {
47  auto dur_s = std::chrono::duration_cast<std::chrono::duration<double>>(dur);
48  return fmt::format(FMT_STRING("{:<18} time: {:<8.4e} s throughput: {:>6.3e} it/s"),
49  name,
50  dur_s.count(),
51  static_cast<double>(iterations) / dur_s.count());
52 }
53 
54 #endif
auto format_duration(std::string_view name, dur_t dur, size_t iterations, size_t block_size)
Definition: benchmark_common.h:42
fmt::format_context::iterator format(const gr::io_signature &iosig, format_context &ctx) const
auto benchmark(functor test, size_t block_size)
Definition: benchmark_common.h:21