str_join_benchmark.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Copyright 2018 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "absl/strings/str_join.h"
  16. #include <string>
  17. #include <vector>
  18. #include <utility>
  19. #include "benchmark/benchmark.h"
  20. namespace {
  21. void BM_Join2_Strings(benchmark::State& state) {
  22. const int string_len = state.range(0);
  23. const int num_strings = state.range(1);
  24. const std::string s(string_len, 'x');
  25. const std::vector<std::string> v(num_strings, s);
  26. for (auto _ : state) {
  27. std::string s = absl::StrJoin(v, "-");
  28. benchmark::DoNotOptimize(s);
  29. }
  30. }
  31. BENCHMARK(BM_Join2_Strings)
  32. ->ArgPair(1 << 0, 1 << 3)
  33. ->ArgPair(1 << 10, 1 << 3)
  34. ->ArgPair(1 << 13, 1 << 3)
  35. ->ArgPair(1 << 0, 1 << 10)
  36. ->ArgPair(1 << 10, 1 << 10)
  37. ->ArgPair(1 << 13, 1 << 10)
  38. ->ArgPair(1 << 0, 1 << 13)
  39. ->ArgPair(1 << 10, 1 << 13)
  40. ->ArgPair(1 << 13, 1 << 13);
  41. void BM_Join2_Ints(benchmark::State& state) {
  42. const int num_ints = state.range(0);
  43. const std::vector<int> v(num_ints, 42);
  44. for (auto _ : state) {
  45. std::string s = absl::StrJoin(v, "-");
  46. benchmark::DoNotOptimize(s);
  47. }
  48. }
  49. BENCHMARK(BM_Join2_Ints)->Range(0, 1 << 13);
  50. void BM_Join2_KeysAndValues(benchmark::State& state) {
  51. const int string_len = state.range(0);
  52. const int num_pairs = state.range(1);
  53. const std::string s(string_len, 'x');
  54. const std::vector<std::pair<std::string, int>> v(num_pairs,
  55. std::make_pair(s, 42));
  56. for (auto _ : state) {
  57. std::string s = absl::StrJoin(v, ",", absl::PairFormatter("="));
  58. benchmark::DoNotOptimize(s);
  59. }
  60. }
  61. BENCHMARK(BM_Join2_KeysAndValues)
  62. ->ArgPair(1 << 0, 1 << 3)
  63. ->ArgPair(1 << 10, 1 << 3)
  64. ->ArgPair(1 << 13, 1 << 3)
  65. ->ArgPair(1 << 0, 1 << 10)
  66. ->ArgPair(1 << 10, 1 << 10)
  67. ->ArgPair(1 << 13, 1 << 10)
  68. ->ArgPair(1 << 0, 1 << 13)
  69. ->ArgPair(1 << 10, 1 << 13)
  70. ->ArgPair(1 << 13, 1 << 13);
  71. void BM_JoinStreamable(benchmark::State& state) {
  72. const int string_len = state.range(0);
  73. const int num_strings = state.range(1);
  74. const std::vector<std::string> v(num_strings, std::string(string_len, 'x'));
  75. for (auto _ : state) {
  76. std::string s = absl::StrJoin(v, "", absl::StreamFormatter());
  77. benchmark::DoNotOptimize(s);
  78. }
  79. }
  80. BENCHMARK(BM_JoinStreamable)
  81. ->ArgPair(0, 0)
  82. ->ArgPair(16, 1)
  83. ->ArgPair(256, 1)
  84. ->ArgPair(16, 16)
  85. ->ArgPair(256, 16)
  86. ->ArgPair(16, 256)
  87. ->ArgPair(256, 256);
  88. } // namespace