benchmark_radix_tree.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #if __cplusplus >= 201103L
4 
5 #include "radix_tree.hpp"
6 #include "trie.hpp"
7 
8 #include <chrono>
9 #include <cstddef>
10 #include <cstdio>
11 #include <random>
12 #include <ratio>
13 #include <vector>
14 
15 const std::size_t nkeys = 10000;
16 const std::size_t nqueries = 1000000;
17 const std::size_t warmup_runs = 10;
18 const std::size_t samples = 10;
19 const std::size_t key_length = 20;
20 const char *chars = "abcdefghijklmnopqrstuvwxyz0123456789";
21 const int chars_len = 36;
22 
23 template <class T>
24 void benchmark_lookup (T &subscriptions_,
25  std::vector<unsigned char *> &queries_)
26 {
27  using namespace std::chrono;
28  std::vector<duration<long, std::nano> > samples_vec;
29  samples_vec.reserve (samples);
30 
31  for (std::size_t run = 0; run < warmup_runs; ++run) {
32  for (auto &query : queries_)
33  subscriptions_.check (query, key_length);
34  }
35 
36  for (std::size_t run = 0; run < samples; ++run) {
37  duration<long, std::nano> interval (0);
38  for (auto &query : queries_) {
39  auto start = steady_clock::now ();
40  subscriptions_.check (query, key_length);
41  auto end = steady_clock::now ();
42  interval += end - start;
43  }
44  samples_vec.push_back (interval / queries_.size ());
45  }
46 
47  std::size_t sum = 0;
48  for (const auto &sample : samples_vec)
49  sum += sample.count ();
50  std::printf ("Average lookup time = %.1lf ns\n",
51  static_cast<double> (sum) / samples);
52 }
53 
54 int main ()
55 {
56  // Generate input set.
57  std::minstd_rand rng (123456789);
58  std::vector<unsigned char *> input_set;
59  std::vector<unsigned char *> queries;
60  input_set.reserve (nkeys);
61  queries.reserve (nqueries);
62 
63  for (std::size_t i = 0; i < nkeys; ++i) {
64  unsigned char *key = new unsigned char[key_length];
65  for (std::size_t j = 0; j < key_length; j++)
66  key[j] = static_cast<unsigned char> (chars[rng () % chars_len]);
67  input_set.emplace_back (key);
68  }
69  for (std::size_t i = 0; i < nqueries; ++i)
70  queries.push_back (input_set[rng () % nkeys]);
71 
72  // Initialize both data structures.
73  //
74  // Keeping initialization out of the benchmarking function helps
75  // heaptrack detect peak memory consumption of the radix tree.
76  zmq::trie_t trie;
77  zmq::radix_tree_t radix_tree;
78  for (auto &key : input_set) {
79  trie.add (key, key_length);
80  radix_tree.add (key, key_length);
81  }
82 
83  // Create a benchmark.
84  std::printf ("keys = %llu, queries = %llu, key size = %llu\n",
85  static_cast<unsigned long long> (nkeys),
86  static_cast<unsigned long long> (nqueries),
87  static_cast<unsigned long long> (key_length));
88  std::puts ("[trie]");
89  benchmark_lookup (trie, queries);
90 
91  std::puts ("[radix_tree]");
92  benchmark_lookup (radix_tree, queries);
93 
94  for (auto &op : input_set)
95  delete[] op;
96 }
97 
98 #else
99 
100 int main ()
101 {
102 }
103 
104 #endif
zmq::trie_t
Definition: trie.hpp:14
end
GLuint GLuint end
Definition: glcorearb.h:2858
T
#define T(upbtypeconst, upbtype, ctype, default_value)
radix_tree.hpp
zmq::radix_tree_t
Definition: radix_tree.hpp:89
trie.hpp
start
GLuint start
Definition: glcorearb.h:2858
zmq::radix_tree_t::add
bool add(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:259
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
i
int i
Definition: gmock-matchers_test.cc:764
main
int main()
Definition: benchmark_radix_tree.cpp:100
samples
GLsizei samples
Definition: glcorearb.h:3468
zmq::trie_t::add
bool add(unsigned char *prefix_, size_t size_)
Definition: trie.cpp:30


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48