ostringstream_benchmark.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/internal/ostringstream.h"
  15. #include <sstream>
  16. #include <string>
  17. #include "benchmark/benchmark.h"
  18. namespace {
  19. enum StringType {
  20. kNone,
  21. kStdString,
  22. };
  23. // Benchmarks for std::ostringstream.
  24. template <StringType kOutput>
  25. void BM_StdStream(benchmark::State& state) {
  26. const int num_writes = state.range(0);
  27. const int bytes_per_write = state.range(1);
  28. const std::string payload(bytes_per_write, 'x');
  29. for (auto _ : state) {
  30. std::ostringstream strm;
  31. benchmark::DoNotOptimize(strm);
  32. for (int i = 0; i != num_writes; ++i) {
  33. strm << payload;
  34. }
  35. switch (kOutput) {
  36. case kNone: {
  37. break;
  38. }
  39. case kStdString: {
  40. std::string s = strm.str();
  41. benchmark::DoNotOptimize(s);
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. // Create the stream, optionally write to it, then destroy it.
  48. BENCHMARK_TEMPLATE(BM_StdStream, kNone)
  49. ->ArgPair(0, 0)
  50. ->ArgPair(1, 16) // 16 bytes is small enough for SSO
  51. ->ArgPair(1, 256) // 256 bytes requires heap allocation
  52. ->ArgPair(1024, 256);
  53. // Create the stream, write to it, get std::string out, then destroy.
  54. BENCHMARK_TEMPLATE(BM_StdStream, kStdString)
  55. ->ArgPair(1, 16) // 16 bytes is small enough for SSO
  56. ->ArgPair(1, 256) // 256 bytes requires heap allocation
  57. ->ArgPair(1024, 256);
  58. // Benchmarks for OStringStream.
  59. template <StringType kOutput>
  60. void BM_CustomStream(benchmark::State& state) {
  61. const int num_writes = state.range(0);
  62. const int bytes_per_write = state.range(1);
  63. const std::string payload(bytes_per_write, 'x');
  64. for (auto _ : state) {
  65. std::string out;
  66. absl::strings_internal::OStringStream strm(&out);
  67. benchmark::DoNotOptimize(strm);
  68. for (int i = 0; i != num_writes; ++i) {
  69. strm << payload;
  70. }
  71. switch (kOutput) {
  72. case kNone: {
  73. break;
  74. }
  75. case kStdString: {
  76. std::string s = out;
  77. benchmark::DoNotOptimize(s);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. // Create the stream, optionally write to it, then destroy it.
  84. BENCHMARK_TEMPLATE(BM_CustomStream, kNone)
  85. ->ArgPair(0, 0)
  86. ->ArgPair(1, 16) // 16 bytes is small enough for SSO
  87. ->ArgPair(1, 256) // 256 bytes requires heap allocation
  88. ->ArgPair(1024, 256);
  89. // Create the stream, write to it, get std::string out, then destroy.
  90. // It's not useful in practice to extract std::string from OStringStream; we
  91. // measure it for completeness.
  92. BENCHMARK_TEMPLATE(BM_CustomStream, kStdString)
  93. ->ArgPair(1, 16) // 16 bytes is small enough for SSO
  94. ->ArgPair(1, 256) // 256 bytes requires heap allocation
  95. ->ArgPair(1024, 256);
  96. } // namespace