ostringstream_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/internal/ostringstream.h"
00016 
00017 #include <sstream>
00018 #include <string>
00019 
00020 #include "benchmark/benchmark.h"
00021 
00022 namespace {
00023 
00024 enum StringType {
00025   kNone,
00026   kStdString,
00027 };
00028 
00029 // Benchmarks for std::ostringstream.
00030 template <StringType kOutput>
00031 void BM_StdStream(benchmark::State& state) {
00032   const int num_writes = state.range(0);
00033   const int bytes_per_write = state.range(1);
00034   const std::string payload(bytes_per_write, 'x');
00035   for (auto _ : state) {
00036     std::ostringstream strm;
00037     benchmark::DoNotOptimize(strm);
00038     for (int i = 0; i != num_writes; ++i) {
00039       strm << payload;
00040     }
00041     switch (kOutput) {
00042       case kNone: {
00043         break;
00044       }
00045       case kStdString: {
00046         std::string s = strm.str();
00047         benchmark::DoNotOptimize(s);
00048         break;
00049       }
00050     }
00051   }
00052 }
00053 
00054 // Create the stream, optionally write to it, then destroy it.
00055 BENCHMARK_TEMPLATE(BM_StdStream, kNone)
00056     ->ArgPair(0, 0)
00057     ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
00058     ->ArgPair(1, 256)  // 256 bytes requires heap allocation
00059     ->ArgPair(1024, 256);
00060 // Create the stream, write to it, get std::string out, then destroy.
00061 BENCHMARK_TEMPLATE(BM_StdStream, kStdString)
00062     ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
00063     ->ArgPair(1, 256)  // 256 bytes requires heap allocation
00064     ->ArgPair(1024, 256);
00065 
00066 // Benchmarks for OStringStream.
00067 template <StringType kOutput>
00068 void BM_CustomStream(benchmark::State& state) {
00069   const int num_writes = state.range(0);
00070   const int bytes_per_write = state.range(1);
00071   const std::string payload(bytes_per_write, 'x');
00072   for (auto _ : state) {
00073     std::string out;
00074     absl::strings_internal::OStringStream strm(&out);
00075     benchmark::DoNotOptimize(strm);
00076     for (int i = 0; i != num_writes; ++i) {
00077       strm << payload;
00078     }
00079     switch (kOutput) {
00080       case kNone: {
00081         break;
00082       }
00083       case kStdString: {
00084         std::string s = out;
00085         benchmark::DoNotOptimize(s);
00086         break;
00087       }
00088     }
00089   }
00090 }
00091 
00092 // Create the stream, optionally write to it, then destroy it.
00093 BENCHMARK_TEMPLATE(BM_CustomStream, kNone)
00094     ->ArgPair(0, 0)
00095     ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
00096     ->ArgPair(1, 256)  // 256 bytes requires heap allocation
00097     ->ArgPair(1024, 256);
00098 // Create the stream, write to it, get std::string out, then destroy.
00099 // It's not useful in practice to extract std::string from OStringStream; we
00100 // measure it for completeness.
00101 BENCHMARK_TEMPLATE(BM_CustomStream, kStdString)
00102     ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
00103     ->ArgPair(1, 256)  // 256 bytes requires heap allocation
00104     ->ArgPair(1024, 256);
00105 
00106 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15