benchmark-set.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Benchmarks for hb_set_t operations.
  3. */
  4. #include "benchmark/benchmark.h"
  5. #include <cassert>
  6. #include <cstdlib>
  7. #include "hb.h"
  8. void RandomSet(unsigned size, unsigned max_value, hb_set_t* out) {
  9. hb_set_clear(out);
  10. srand(size * max_value);
  11. for (unsigned i = 0; i < size; i++) {
  12. while (true) {
  13. unsigned next = rand() % max_value;
  14. if (hb_set_has (out, next)) continue;
  15. hb_set_add(out, next);
  16. break;
  17. }
  18. }
  19. }
  20. // TODO(garretrieger): benchmark union/subtract/intersection etc.
  21. /* Insert a 1000 values into set of varying sizes. */
  22. static void BM_SetInsert_1000(benchmark::State& state) {
  23. unsigned set_size = state.range(0);
  24. unsigned max_value = state.range(0) * state.range(1);
  25. hb_set_t* original = hb_set_create ();
  26. RandomSet(set_size, max_value, original);
  27. assert(hb_set_get_population(original) == set_size);
  28. for (auto _ : state) {
  29. state.PauseTiming ();
  30. hb_set_t* data = hb_set_copy(original);
  31. state.ResumeTiming ();
  32. for (int i = 0; i < 1000; i++) {
  33. hb_set_add(data, i * 2654435761u % max_value);
  34. }
  35. hb_set_destroy(data);
  36. }
  37. hb_set_destroy(original);
  38. }
  39. BENCHMARK(BM_SetInsert_1000)
  40. ->Unit(benchmark::kMicrosecond)
  41. ->Ranges(
  42. {{1 << 10, 1 << 16}, // Set Size
  43. {2, 512}}); // Density
  44. /* Insert a 1000 values into set of varying sizes. */
  45. static void BM_SetOrderedInsert_1000(benchmark::State& state) {
  46. unsigned set_size = state.range(0);
  47. unsigned max_value = state.range(0) * state.range(1);
  48. hb_set_t* original = hb_set_create ();
  49. RandomSet(set_size, max_value, original);
  50. assert(hb_set_get_population(original) == set_size);
  51. for (auto _ : state) {
  52. state.PauseTiming ();
  53. hb_set_t* data = hb_set_copy(original);
  54. state.ResumeTiming ();
  55. for (int i = 0; i < 1000; i++) {
  56. hb_set_add(data, i);
  57. }
  58. hb_set_destroy(data);
  59. }
  60. hb_set_destroy(original);
  61. }
  62. BENCHMARK(BM_SetOrderedInsert_1000)
  63. ->Unit(benchmark::kMicrosecond)
  64. ->Ranges(
  65. {{1 << 10, 1 << 16}, // Set Size
  66. {2, 512}}); // Density
  67. /* Single value lookup on sets of various sizes. */
  68. static void BM_SetLookup(benchmark::State& state, unsigned interval) {
  69. unsigned set_size = state.range(0);
  70. unsigned max_value = state.range(0) * state.range(1);
  71. hb_set_t* original = hb_set_create ();
  72. RandomSet(set_size, max_value, original);
  73. assert(hb_set_get_population(original) == set_size);
  74. auto needle = max_value / 2;
  75. for (auto _ : state) {
  76. benchmark::DoNotOptimize(
  77. hb_set_has (original, (needle += interval) % max_value));
  78. }
  79. hb_set_destroy(original);
  80. }
  81. BENCHMARK_CAPTURE(BM_SetLookup, ordered, 3)
  82. ->Ranges(
  83. {{1 << 10, 1 << 16}, // Set Size
  84. {2, 512}}); // Density
  85. BENCHMARK_CAPTURE(BM_SetLookup, random, 12345)
  86. ->Ranges(
  87. {{1 << 10, 1 << 16}, // Set Size
  88. {2, 512}}); // Density
  89. /* Full iteration of sets of varying sizes. */
  90. static void BM_SetIteration(benchmark::State& state) {
  91. unsigned set_size = state.range(0);
  92. unsigned max_value = state.range(0) * state.range(1);
  93. hb_set_t* original = hb_set_create ();
  94. RandomSet(set_size, max_value, original);
  95. assert(hb_set_get_population(original) == set_size);
  96. hb_codepoint_t cp = HB_SET_VALUE_INVALID;
  97. for (auto _ : state) {
  98. hb_set_next (original, &cp);
  99. }
  100. hb_set_destroy(original);
  101. }
  102. BENCHMARK(BM_SetIteration)
  103. ->Ranges(
  104. {{1 << 10, 1 << 16}, // Set Size
  105. {2, 512}}); // Density
  106. /* Set copy. */
  107. static void BM_SetCopy(benchmark::State& state) {
  108. unsigned set_size = state.range(0);
  109. unsigned max_value = state.range(0) * state.range(1);
  110. hb_set_t* original = hb_set_create ();
  111. RandomSet(set_size, max_value, original);
  112. assert(hb_set_get_population(original) == set_size);
  113. for (auto _ : state) {
  114. hb_set_t *s = hb_set_create ();
  115. hb_set_set (s, original);
  116. hb_set_destroy (s);
  117. }
  118. hb_set_destroy(original);
  119. }
  120. BENCHMARK(BM_SetCopy)
  121. ->Unit(benchmark::kMicrosecond)
  122. ->Ranges(
  123. {{1 << 10, 1 << 16}, // Set Size
  124. {2, 512}}); // Density
  125. BENCHMARK_MAIN();