-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_cat.cpp
174 lines (155 loc) · 4.25 KB
/
string_cat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2020 The Division Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author dietoad@gmail.com && firedtoad@gmail.com
#include <absl/strings/str_cat.h>
#include <absl/strings/str_format.h>
#include <benchmark/benchmark.h>
#include <boost/algorithm/string.hpp>
#include <fmt/format.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/repeated_ptr_field.h>
#include <random>
#include <sstream>
std::string tag = "1234567890123456";
std::string tag1 = "1234567890123456";
static void BenchStrNCat(benchmark::State &state)
{
for (auto _ : state)
{
char dest[128]{};
strncat(dest, tag.c_str(), tag.size());
strncat(&dest[tag.size()], ":", 2);
strncat(&dest[tag.size() + 1], tag1.c_str(), tag1.size());
benchmark::DoNotOptimize(dest);
}
}
BENCHMARK(BenchStrNCat);
static void BenchStdStringCat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = tag;
r += ":";
r += tag1;
r += tag1;
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchStdStringCat);
static void BenchStreamStringCat(benchmark::State &state)
{
for (auto _ : state)
{
std::stringstream ss;
ss << tag << ":" << tag1 << tag1;
auto r = ss.str();
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchStreamStringCat);
static void BenchStaticStreamStringCat(benchmark::State &state)
{
for (auto _ : state)
{
thread_local static std::stringstream ss;
ss.str("");
ss.clear();
ss << tag << ":" << tag1 << tag1;
auto r = ss.str();
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchStaticStreamStringCat);
static void BenchFmtAppend(benchmark::State &state)
{
for (auto _ : state)
{
fmt::basic_memory_buffer<char> buffer;
buffer.append(tag);
buffer.push_back(':');
buffer.append(tag1);
buffer.append(tag1);
auto r=fmt::to_string(buffer);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchFmtAppend);
static void BenchBoostStrCat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = boost::join<std::vector<std::string>>({tag, ":", tag1}, "");
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchBoostStrCat);
static void BenchAbStrCat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = absl::StrCat(tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchAbStrCat);
static void BenchPBStrCat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = google::protobuf::StrCat(tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchPBStrCat);
static void BenchAbStrFormat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = absl::StrFormat("%s%s%s%s", tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchAbStrFormat);
static void BenchFormat(benchmark::State &state)
{
for (auto _ : state)
{
auto r = fmt::format("{}{}{}{}", tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchFormat);
#include "utils/rss.h"
const int SIZE=1024*100;
int main(int argc, char **argv)
{
tag = std::to_string(std::mt19937_64{std::random_device{}()}());
tag1 = std::to_string(std::mt19937_64{std::random_device{}()}());
rusage rUsage;
FillRSS(rUsage);
google::protobuf::RepeatedField<int> rf;
for(auto i{0};i<SIZE;i++)
{
rf.Add(i);
}
PrintUsage(rUsage);
FillRSS(rUsage);
google::protobuf::RepeatedField<int> rf1;
rf1.Resize(SIZE,{});
PrintUsage(rUsage);
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}