reserve.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <benchmark/benchmark.h>
  2. #include <library/cpp/yson/node/node_builder.h>
  3. using namespace NYT;
  4. static void BM_Map(benchmark::State& state, const std::tuple<int, bool>& input) {
  5. for (auto _ : state) {
  6. TNode node;
  7. TNodeBuilder builder(&node);
  8. if (std::get<1>(input)) {
  9. builder.OnBeginMap(std::get<0>(input));
  10. } else {
  11. builder.OnBeginMap();
  12. }
  13. for (auto i = 0; i < std::get<0>(input); ++i) {
  14. builder.OnKeyedItem(ToString(i));
  15. builder.OnUint64Scalar(i);
  16. }
  17. builder.OnEndMap();
  18. }
  19. }
  20. static void BM_List(benchmark::State& state, const std::tuple<int, bool>& input) {
  21. for (auto _ : state) {
  22. TNode node;
  23. TNodeBuilder builder(&node);
  24. if (std::get<1>(input)) {
  25. builder.OnBeginList(std::get<0>(input));
  26. } else {
  27. builder.OnBeginList();
  28. }
  29. for (auto i = 0; i < std::get<0>(input); ++i) {
  30. builder.OnListItem();
  31. builder.OnUint64Scalar(i);
  32. }
  33. builder.OnEndList();
  34. }
  35. }
  36. BENCHMARK_CAPTURE(BM_Map, map_10, std::make_tuple(10u, false));
  37. BENCHMARK_CAPTURE(BM_Map, map_100, std::make_tuple(100u, false));
  38. BENCHMARK_CAPTURE(BM_Map, map_300, std::make_tuple(300u, false));
  39. BENCHMARK_CAPTURE(BM_Map, map_500, std::make_tuple(500u, false));
  40. BENCHMARK_CAPTURE(BM_Map, map_1000, std::make_tuple(1000u, false));
  41. BENCHMARK_CAPTURE(BM_Map, map_reserve_10, std::make_tuple(10u, true));
  42. BENCHMARK_CAPTURE(BM_Map, map_reserve_100, std::make_tuple(100u, true));
  43. BENCHMARK_CAPTURE(BM_Map, map_reserve_300, std::make_tuple(300u, true));
  44. BENCHMARK_CAPTURE(BM_Map, map_reserve_500, std::make_tuple(500u, true));
  45. BENCHMARK_CAPTURE(BM_Map, map_reserve_1000, std::make_tuple(1000u, true));
  46. BENCHMARK_CAPTURE(BM_List, list_10, std::make_tuple(10u, false));
  47. BENCHMARK_CAPTURE(BM_List, list_100, std::make_tuple(100u, false));
  48. BENCHMARK_CAPTURE(BM_List, list_300, std::make_tuple(300u, false));
  49. BENCHMARK_CAPTURE(BM_List, list_500, std::make_tuple(500u, false));
  50. BENCHMARK_CAPTURE(BM_List, list_1000, std::make_tuple(1000u, false));
  51. BENCHMARK_CAPTURE(BM_List, list_reserve_10, std::make_tuple(10u, true));
  52. BENCHMARK_CAPTURE(BM_List, list_reserve_100, std::make_tuple(100u, true));
  53. BENCHMARK_CAPTURE(BM_List, list_reserve_300, std::make_tuple(300u, true));
  54. BENCHMARK_CAPTURE(BM_List, list_reserve_500, std::make_tuple(500u, true));
  55. BENCHMARK_CAPTURE(BM_List, list_reserve_1000, std::make_tuple(1000u, true));