str_replace_benchmark.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/str_replace.h"
  15. #include <cstring>
  16. #include <string>
  17. #include "benchmark/benchmark.h"
  18. #include "absl/base/internal/raw_logging.h"
  19. namespace {
  20. std::string* big_string;
  21. std::string* after_replacing_the;
  22. std::string* after_replacing_many;
  23. struct Replacement {
  24. const char* needle;
  25. const char* replacement;
  26. } replacements[] = {
  27. {"the", "box"}, //
  28. {"brown", "quick"}, //
  29. {"jumped", "liquored"}, //
  30. {"dozen", "brown"}, //
  31. {"lazy", "pack"}, //
  32. {"liquor", "shakes"}, //
  33. };
  34. // Here, we set up a string for use in global-replace benchmarks.
  35. // We started with a million blanks, and then deterministically insert
  36. // 10,000 copies each of two pangrams. The result is a string that is
  37. // 40% blank space and 60% these words. 'the' occurs 18,247 times and
  38. // all the substitutions together occur 49,004 times.
  39. //
  40. // We then create "after_replacing_the" to be a string that is a result of
  41. // replacing "the" with "box" in big_string.
  42. //
  43. // And then we create "after_replacing_many" to be a string that is result
  44. // of preferring several substitutions.
  45. void SetUpStrings() {
  46. if (big_string == nullptr) {
  47. size_t r = 0;
  48. big_string = new std::string(1000 * 1000, ' ');
  49. for (std::string phrase : {"the quick brown fox jumped over the lazy dogs",
  50. "pack my box with the five dozen liquor jugs"}) {
  51. for (int i = 0; i < 10 * 1000; ++i) {
  52. r = r * 237 + 41; // not very random.
  53. memcpy(&(*big_string)[r % (big_string->size() - phrase.size())],
  54. phrase.data(), phrase.size());
  55. }
  56. }
  57. // big_string->resize(50);
  58. // OK, we've set up the string, now let's set up expectations - first by
  59. // just replacing "the" with "box"
  60. after_replacing_the = new std::string(*big_string);
  61. for (size_t pos = 0;
  62. (pos = after_replacing_the->find("the", pos)) != std::string::npos;) {
  63. memcpy(&(*after_replacing_the)[pos], "box", 3);
  64. }
  65. // And then with all the replacements.
  66. after_replacing_many = new std::string(*big_string);
  67. for (size_t pos = 0;;) {
  68. size_t next_pos = static_cast<size_t>(-1);
  69. const char* needle_string = nullptr;
  70. const char* replacement_string = nullptr;
  71. for (const auto& r : replacements) {
  72. auto needlepos = after_replacing_many->find(r.needle, pos);
  73. if (needlepos != std::string::npos && needlepos < next_pos) {
  74. next_pos = needlepos;
  75. needle_string = r.needle;
  76. replacement_string = r.replacement;
  77. }
  78. }
  79. if (next_pos > after_replacing_many->size()) break;
  80. after_replacing_many->replace(next_pos, strlen(needle_string),
  81. replacement_string);
  82. next_pos += strlen(replacement_string);
  83. pos = next_pos;
  84. }
  85. }
  86. }
  87. void BM_StrReplaceAllOneReplacement(benchmark::State& state) {
  88. SetUpStrings();
  89. std::string src = *big_string;
  90. for (auto _ : state) {
  91. std::string dest = absl::StrReplaceAll(src, {{"the", "box"}});
  92. ABSL_RAW_CHECK(dest == *after_replacing_the,
  93. "not benchmarking intended behavior");
  94. }
  95. }
  96. BENCHMARK(BM_StrReplaceAllOneReplacement);
  97. void BM_StrReplaceAll(benchmark::State& state) {
  98. SetUpStrings();
  99. std::string src = *big_string;
  100. for (auto _ : state) {
  101. std::string dest = absl::StrReplaceAll(src, {{"the", "box"},
  102. {"brown", "quick"},
  103. {"jumped", "liquored"},
  104. {"dozen", "brown"},
  105. {"lazy", "pack"},
  106. {"liquor", "shakes"}});
  107. ABSL_RAW_CHECK(dest == *after_replacing_many,
  108. "not benchmarking intended behavior");
  109. }
  110. }
  111. BENCHMARK(BM_StrReplaceAll);
  112. } // namespace