Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
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
00055 BENCHMARK_TEMPLATE(BM_StdStream, kNone)
00056 ->ArgPair(0, 0)
00057 ->ArgPair(1, 16)
00058 ->ArgPair(1, 256)
00059 ->ArgPair(1024, 256);
00060
00061 BENCHMARK_TEMPLATE(BM_StdStream, kStdString)
00062 ->ArgPair(1, 16)
00063 ->ArgPair(1, 256)
00064 ->ArgPair(1024, 256);
00065
00066
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
00093 BENCHMARK_TEMPLATE(BM_CustomStream, kNone)
00094 ->ArgPair(0, 0)
00095 ->ArgPair(1, 16)
00096 ->ArgPair(1, 256)
00097 ->ArgPair(1024, 256);
00098
00099
00100
00101 BENCHMARK_TEMPLATE(BM_CustomStream, kStdString)
00102 ->ArgPair(1, 16)
00103 ->ArgPair(1, 256)
00104 ->ArgPair(1024, 256);
00105
00106 }