2022-04-20 21:33:04 +02:00
|
|
|
/*
|
|
|
|
* Benchmarks for hb_set_t operations.
|
|
|
|
*/
|
2022-04-20 20:36:35 +02:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include "hb.h"
|
|
|
|
|
|
|
|
void RandomSet(unsigned size, unsigned max_value, hb_set_t* out) {
|
|
|
|
hb_set_clear(out);
|
|
|
|
|
|
|
|
srand(size * max_value);
|
|
|
|
for (unsigned i = 0; i < size; i++) {
|
|
|
|
while (true) {
|
|
|
|
unsigned next = rand() % max_value;
|
|
|
|
if (hb_set_has (out, next)) continue;
|
|
|
|
|
|
|
|
hb_set_add(out, next);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:33:04 +02:00
|
|
|
// TODO(garretrieger): benchmark union/subtract/intersection etc.
|
|
|
|
|
2022-04-20 23:19:54 +02:00
|
|
|
/* Insert a 1000 values into set of varying sizes. */
|
|
|
|
static void BM_SetInsert_1000(benchmark::State& state) {
|
2022-04-20 20:36:35 +02:00
|
|
|
unsigned set_size = state.range(0);
|
|
|
|
unsigned max_value = state.range(0) * state.range(1);
|
|
|
|
|
|
|
|
hb_set_t* original = hb_set_create ();
|
|
|
|
RandomSet(set_size, max_value, original);
|
|
|
|
assert(hb_set_get_population(original) == set_size);
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
hb_set_t* data = hb_set_copy(original);
|
2022-04-20 23:19:54 +02:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
hb_set_add(data, rand() % max_value);
|
|
|
|
}
|
|
|
|
hb_set_destroy(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_set_destroy(original);
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_SetInsert_1000)
|
|
|
|
->Unit(benchmark::kMicrosecond)
|
2022-04-20 23:34:45 +02:00
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
2022-04-20 23:19:54 +02:00
|
|
|
|
|
|
|
/* Insert a 1000 values into set of varying sizes. */
|
|
|
|
static void BM_SetOrderedInsert_1000(benchmark::State& state) {
|
|
|
|
unsigned set_size = state.range(0);
|
|
|
|
unsigned max_value = state.range(0) * state.range(1);
|
2022-04-20 20:36:35 +02:00
|
|
|
|
2022-04-20 23:19:54 +02:00
|
|
|
hb_set_t* original = hb_set_create ();
|
|
|
|
RandomSet(set_size, max_value, original);
|
|
|
|
assert(hb_set_get_population(original) == set_size);
|
2022-04-20 20:36:35 +02:00
|
|
|
|
2022-04-20 23:19:54 +02:00
|
|
|
for (auto _ : state) {
|
|
|
|
hb_set_t* data = hb_set_copy(original);
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
hb_set_add(data, i);
|
|
|
|
}
|
2022-04-20 20:36:35 +02:00
|
|
|
hb_set_destroy(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_set_destroy(original);
|
|
|
|
}
|
2022-04-20 23:19:54 +02:00
|
|
|
BENCHMARK(BM_SetOrderedInsert_1000)
|
|
|
|
->Unit(benchmark::kMicrosecond)
|
2022-04-20 23:34:45 +02:00
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
2022-04-20 20:36:35 +02:00
|
|
|
|
2022-04-20 21:15:03 +02:00
|
|
|
/* Single value lookup on sets of various sizes. */
|
2022-04-29 21:37:46 +02:00
|
|
|
static void BM_SetLookup(benchmark::State& state, unsigned interval) {
|
2022-04-20 21:15:03 +02:00
|
|
|
unsigned set_size = state.range(0);
|
|
|
|
unsigned max_value = state.range(0) * state.range(1);
|
|
|
|
|
|
|
|
hb_set_t* original = hb_set_create ();
|
|
|
|
RandomSet(set_size, max_value, original);
|
|
|
|
assert(hb_set_get_population(original) == set_size);
|
|
|
|
|
2022-04-29 20:23:53 +02:00
|
|
|
auto needle = max_value / 2;
|
2022-04-20 21:15:03 +02:00
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(
|
2022-04-29 21:37:46 +02:00
|
|
|
hb_set_has (original, (needle += interval) % max_value));
|
2022-04-20 21:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hb_set_destroy(original);
|
|
|
|
}
|
2022-04-29 21:37:46 +02:00
|
|
|
BENCHMARK_CAPTURE(BM_SetLookup, ordered, 3)
|
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
|
|
|
BENCHMARK_CAPTURE(BM_SetLookup, random, 12345)
|
2022-04-20 23:34:45 +02:00
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
2022-04-20 21:15:03 +02:00
|
|
|
|
|
|
|
/* Full iteration of sets of varying sizes. */
|
|
|
|
static void BM_SetIteration(benchmark::State& state) {
|
|
|
|
unsigned set_size = state.range(0);
|
|
|
|
unsigned max_value = state.range(0) * state.range(1);
|
|
|
|
|
|
|
|
hb_set_t* original = hb_set_create ();
|
|
|
|
RandomSet(set_size, max_value, original);
|
|
|
|
assert(hb_set_get_population(original) == set_size);
|
|
|
|
|
2022-04-20 23:19:54 +02:00
|
|
|
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
|
2022-04-20 21:15:03 +02:00
|
|
|
for (auto _ : state) {
|
2022-04-20 23:19:54 +02:00
|
|
|
hb_set_next (original, &cp);
|
2022-04-20 21:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hb_set_destroy(original);
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_SetIteration)
|
2022-04-20 23:34:45 +02:00
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
2022-04-20 21:15:03 +02:00
|
|
|
|
2022-05-19 22:12:42 +02:00
|
|
|
/* Set copy. */
|
|
|
|
static void BM_SetCopy(benchmark::State& state) {
|
|
|
|
unsigned set_size = state.range(0);
|
|
|
|
unsigned max_value = state.range(0) * state.range(1);
|
|
|
|
|
|
|
|
hb_set_t* original = hb_set_create ();
|
|
|
|
RandomSet(set_size, max_value, original);
|
|
|
|
assert(hb_set_get_population(original) == set_size);
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
hb_set_t *s = hb_set_create ();
|
|
|
|
hb_set_set (s, original);
|
|
|
|
hb_set_destroy (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_set_destroy(original);
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_SetCopy)
|
|
|
|
->Unit(benchmark::kMicrosecond)
|
|
|
|
->Ranges(
|
|
|
|
{{1 << 10, 1 << 16}, // Set Size
|
|
|
|
{2, 512}}); // Density
|
|
|
|
|
2022-04-20 20:36:35 +02:00
|
|
|
BENCHMARK_MAIN();
|