1 /*
2 * Benchmarks for hb_map_t operations.
3 */
4 #include "benchmark/benchmark.h"
5
6 #include <cassert>
7 #include <cstdlib>
8 #include "hb.h"
9
RandomMap(unsigned size,hb_map_t * out)10 void RandomMap(unsigned size, hb_map_t* out) {
11 hb_map_clear(out);
12
13 srand(size);
14 for (unsigned i = 0; i < size; i++) {
15 while (true) {
16 hb_codepoint_t next = rand();
17 if (hb_map_has (out, next)) continue;
18
19 hb_map_set (out, next, rand ());
20 break;
21 }
22 }
23 }
24
25 /* Insert a single value into map of varying sizes. */
BM_MapInsert(benchmark::State & state)26 static void BM_MapInsert(benchmark::State& state) {
27 unsigned map_size = state.range(0);
28
29 hb_map_t* original = hb_map_create ();
30 RandomMap(map_size, original);
31 assert(hb_map_get_population(original) == map_size);
32
33 auto needle = map_size / 2;
34 auto v = 0;
35 for (auto _ : state) {
36 // TODO(garretrieger): create a copy of the original map.
37 // Needs a hb_map_copy(..) in public api.
38
39 hb_map_set (original, needle++, v++);
40 }
41
42 hb_map_destroy(original);
43 }
44 BENCHMARK(BM_MapInsert)
45 ->Range(1 << 4, 1 << 20);
46
47 /* Single value lookup on map of various sizes. */
BM_MapLookup(benchmark::State & state)48 static void BM_MapLookup(benchmark::State& state) {
49 unsigned map_size = state.range(0);
50
51 hb_map_t* original = hb_map_create ();
52 RandomMap(map_size, original);
53 assert(hb_map_get_population(original) == map_size);
54
55 auto needle = map_size / 2;
56
57 for (auto _ : state) {
58 benchmark::DoNotOptimize(
59 hb_map_get (original, needle++));
60 }
61
62 hb_map_destroy(original);
63 }
64 BENCHMARK(BM_MapLookup)
65 ->Range(1 << 4, 1 << 20); // Map size
66
67
68 BENCHMARK_MAIN();
69