benchmark-map.cc 1.5 KB

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