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/str_cat.h"
00016
00017 #include <cstdint>
00018 #include <string>
00019
00020 #include "benchmark/benchmark.h"
00021 #include "absl/strings/substitute.h"
00022
00023 namespace {
00024
00025 const char kStringOne[] = "Once Upon A Time, ";
00026 const char kStringTwo[] = "There was a std::string benchmark";
00027
00028
00029
00030 inline int IncrementAlternatingSign(int i) {
00031 return i > 0 ? -i : 1 - i;
00032 }
00033
00034 void BM_Sum_By_StrCat(benchmark::State& state) {
00035 int i = 0;
00036 char foo[100];
00037 for (auto _ : state) {
00038
00039 strcpy(foo, absl::StrCat(kStringOne, i, kStringTwo, i * 65536ULL).c_str());
00040 int sum = 0;
00041 for (char* f = &foo[0]; *f != 0; ++f) {
00042 sum += *f;
00043 }
00044 benchmark::DoNotOptimize(sum);
00045 i = IncrementAlternatingSign(i);
00046 }
00047 }
00048 BENCHMARK(BM_Sum_By_StrCat);
00049
00050 void BM_StrCat_By_snprintf(benchmark::State& state) {
00051 int i = 0;
00052 char on_stack[1000];
00053 for (auto _ : state) {
00054 snprintf(on_stack, sizeof(on_stack), "%s %s:%d", kStringOne, kStringTwo, i);
00055 i = IncrementAlternatingSign(i);
00056 }
00057 }
00058 BENCHMARK(BM_StrCat_By_snprintf);
00059
00060 void BM_StrCat_By_Strings(benchmark::State& state) {
00061 int i = 0;
00062 for (auto _ : state) {
00063 std::string result =
00064 std::string(kStringOne) + " " + kStringTwo + ":" + absl::StrCat(i);
00065 benchmark::DoNotOptimize(result);
00066 i = IncrementAlternatingSign(i);
00067 }
00068 }
00069 BENCHMARK(BM_StrCat_By_Strings);
00070
00071 void BM_StrCat_By_StringOpPlus(benchmark::State& state) {
00072 int i = 0;
00073 for (auto _ : state) {
00074 std::string result = kStringOne;
00075 result += " ";
00076 result += kStringTwo;
00077 result += ":";
00078 result += absl::StrCat(i);
00079 benchmark::DoNotOptimize(result);
00080 i = IncrementAlternatingSign(i);
00081 }
00082 }
00083 BENCHMARK(BM_StrCat_By_StringOpPlus);
00084
00085 void BM_StrCat_By_StrCat(benchmark::State& state) {
00086 int i = 0;
00087 for (auto _ : state) {
00088 std::string result = absl::StrCat(kStringOne, " ", kStringTwo, ":", i);
00089 benchmark::DoNotOptimize(result);
00090 i = IncrementAlternatingSign(i);
00091 }
00092 }
00093 BENCHMARK(BM_StrCat_By_StrCat);
00094
00095 void BM_HexCat_By_StrCat(benchmark::State& state) {
00096 int i = 0;
00097 for (auto _ : state) {
00098 std::string result =
00099 absl::StrCat(kStringOne, " ", absl::Hex(int64_t{i} + 0x10000000));
00100 benchmark::DoNotOptimize(result);
00101 i = IncrementAlternatingSign(i);
00102 }
00103 }
00104 BENCHMARK(BM_HexCat_By_StrCat);
00105
00106 void BM_HexCat_By_Substitute(benchmark::State& state) {
00107 int i = 0;
00108 for (auto _ : state) {
00109 std::string result = absl::Substitute(
00110 "$0 $1", kStringOne, reinterpret_cast<void*>(int64_t{i} + 0x10000000));
00111 benchmark::DoNotOptimize(result);
00112 i = IncrementAlternatingSign(i);
00113 }
00114 }
00115 BENCHMARK(BM_HexCat_By_Substitute);
00116
00117 void BM_FloatToString_By_StrCat(benchmark::State& state) {
00118 int i = 0;
00119 float foo = 0.0f;
00120 for (auto _ : state) {
00121 std::string result = absl::StrCat(foo += 1.001f, " != ", int64_t{i});
00122 benchmark::DoNotOptimize(result);
00123 i = IncrementAlternatingSign(i);
00124 }
00125 }
00126 BENCHMARK(BM_FloatToString_By_StrCat);
00127
00128 void BM_DoubleToString_By_SixDigits(benchmark::State& state) {
00129 int i = 0;
00130 double foo = 0.0;
00131 for (auto _ : state) {
00132 std::string result =
00133 absl::StrCat(absl::SixDigits(foo += 1.001), " != ", int64_t{i});
00134 benchmark::DoNotOptimize(result);
00135 i = IncrementAlternatingSign(i);
00136 }
00137 }
00138 BENCHMARK(BM_DoubleToString_By_SixDigits);
00139
00140 }