GNU Radio Manual and C++ API Reference  3.10.9.1
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 /* ensure that tweakme.h is included before the bundled spdlog/fmt header, see
14  * https://github.com/gabime/spdlog/issues/2922 */
15 #include <spdlog/tweakme.h>
16 
17 #include <spdlog/fmt/fmt.h>
18 #include <string_view>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdlib>
22 #include <vector>
23 
24 template <typename functor>
25 [[nodiscard]] auto benchmark(functor test, size_t block_size)
26 {
27  using namespace std::chrono;
28  std::vector<float> outp(2 * block_size);
29  float* output = outp.data();
30  float *x = &output[0], *y = &output[block_size];
31 
32  // touch memory
33  memset(output, 0, 2 * block_size * sizeof(float));
34 
35  auto before = high_resolution_clock::now();
36  // do the actual work
37 
38  test(x, y);
39 
40  auto after = high_resolution_clock::now();
41  // get ending CPU usage
42  auto dur = duration_cast<duration<double, std::ratio<1, 1>>>(after - before);
43  return dur;
44 }
45 template <typename dur_t>
46 auto format_duration(std::string_view name,
47  dur_t dur,
48  size_t iterations,
49  size_t block_size)
50 {
51  auto dur_s = std::chrono::duration_cast<std::chrono::duration<double>>(dur);
52  return fmt::format(FMT_STRING("{:<18} time: {:<8.4e} s throughput: {:>6.3e} it/s"),
53  name,
54  dur_s.count(),
55  static_cast<double>(iterations) / dur_s.count());
56 }
57 
58 #endif
auto format_duration(std::string_view name, dur_t dur, size_t iterations, size_t block_size)
Definition: benchmark_common.h:46
auto benchmark(functor test, size_t block_size)
Definition: benchmark_common.h:25
fmt::format_context::iterator format(const gr::io_signature &iosig, format_context &ctx) const