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 
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;
37  benchmark::DoNotOptimize(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();
47  benchmark::DoNotOptimize(s);
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) {
73  std::string out;
75  benchmark::DoNotOptimize(strm);
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;
85  benchmark::DoNotOptimize(s);
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
std::string * out
Definition: parser_test.cc:293


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:19