abseil-cpp/absl/strings/internal/ostringstream_benchmark.cc
Go to the documentation of this file.
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 
15 #include "absl/strings/internal/ostringstream.h"
16 
17 #include <sstream>
18 #include <string>
19 
20 #include "benchmark/benchmark.h"
21 
22 namespace {
23 
24 enum StringType {
25  kNone,
26  kStdString,
27 };
28 
29 // Benchmarks for std::ostringstream.
30 template <StringType kOutput>
31 void BM_StdStream(benchmark::State& state) {
32  const int num_writes = state.range(0);
33  const int bytes_per_write = state.range(1);
34  const std::string payload(bytes_per_write, 'x');
35  for (auto _ : state) {
36  std::ostringstream strm;
38  for (int i = 0; i != num_writes; ++i) {
39  strm << payload;
40  }
41  switch (kOutput) {
42  case kNone: {
43  break;
44  }
45  case kStdString: {
46  std::string s = strm.str();
48  break;
49  }
50  }
51  }
52 }
53 
54 // Create the stream, optionally write to it, then destroy it.
55 BENCHMARK_TEMPLATE(BM_StdStream, kNone)
56  ->ArgPair(0, 0)
57  ->ArgPair(1, 16) // 16 bytes is small enough for SSO
58  ->ArgPair(1, 256) // 256 bytes requires heap allocation
59  ->ArgPair(1024, 256);
60 // Create the stream, write to it, get std::string out, then destroy.
61 BENCHMARK_TEMPLATE(BM_StdStream, kStdString)
62  ->ArgPair(1, 16) // 16 bytes is small enough for SSO
63  ->ArgPair(1, 256) // 256 bytes requires heap allocation
64  ->ArgPair(1024, 256);
65 
66 // Benchmarks for OStringStream.
67 template <StringType kOutput>
68 void BM_CustomStream(benchmark::State& state) {
69  const int num_writes = state.range(0);
70  const int bytes_per_write = state.range(1);
71  const std::string payload(bytes_per_write, 'x');
72  for (auto _ : state) {
76  for (int i = 0; i != num_writes; ++i) {
77  strm << payload;
78  }
79  switch (kOutput) {
80  case kNone: {
81  break;
82  }
83  case kStdString: {
84  std::string s = out;
86  break;
87  }
88  }
89  }
90 }
91 
92 // Create the stream, optionally write to it, then destroy it.
93 BENCHMARK_TEMPLATE(BM_CustomStream, kNone)
94  ->ArgPair(0, 0)
95  ->ArgPair(1, 16) // 16 bytes is small enough for SSO
96  ->ArgPair(1, 256) // 256 bytes requires heap allocation
97  ->ArgPair(1024, 256);
98 // Create the stream, write to it, get std::string out, then destroy.
99 // It's not useful in practice to extract std::string from OStringStream; we
100 // measure it for completeness.
101 BENCHMARK_TEMPLATE(BM_CustomStream, kStdString)
102  ->ArgPair(1, 16) // 16 bytes is small enough for SSO
103  ->ArgPair(1, 256) // 256 bytes requires heap allocation
104  ->ArgPair(1024, 256);
105 
106 } // namespace
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::strings_internal::OStringStream
Definition: abseil-cpp/absl/strings/internal/ostringstream.h:63
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
absl::FormatConversionChar::s
@ s
absl::flags_internal::HelpMode::kNone
@ kNone
BENCHMARK_TEMPLATE
#define BENCHMARK_TEMPLATE(n, a)
Definition: benchmark/include/benchmark/benchmark.h:1231
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf.internal::StringType
StringType
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_table_driven_lite.h:53
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
kOutput
static const uint8_t kOutput[]
Definition: chacha_test.cc:132
gen_server_registered_method_bad_client_test_body.payload
list payload
Definition: gen_server_registered_method_bad_client_test_body.py:40
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:36