20 #include "benchmark/benchmark.h" 87 constexpr
int kHaystackSize = 10000;
88 constexpr int64_t kHaystackSize64 = kHaystackSize;
89 const char* MakeHaystack() {
90 char* haystack =
new char[kHaystackSize];
91 for (
int i = 0;
i < kHaystackSize - 1; ++
i) haystack[
i] =
'a';
92 haystack[kHaystackSize - 1] =
'b';
95 const char*
const kHaystack = MakeHaystack();
97 void BM_Memmem(benchmark::State& state) {
98 for (
auto _ : state) {
99 benchmark::DoNotOptimize(
102 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
104 BENCHMARK(BM_Memmem);
106 void BM_MemmemMedium(benchmark::State& state) {
107 for (
auto _ : state) {
108 benchmark::DoNotOptimize(
111 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
113 BENCHMARK(BM_MemmemMedium);
115 void BM_MemmemPathological(benchmark::State& state) {
116 for (
auto _ : state) {
118 kHaystack, kHaystackSize, kHaystack + kHaystackSize / 2,
119 kHaystackSize - kHaystackSize / 2));
121 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
123 BENCHMARK(BM_MemmemPathological);
125 void BM_Memcasemem(benchmark::State& state) {
126 for (
auto _ : state) {
127 benchmark::DoNotOptimize(
130 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
132 BENCHMARK(BM_Memcasemem);
134 void BM_MemcasememMedium(benchmark::State& state) {
135 for (
auto _ : state) {
136 benchmark::DoNotOptimize(
139 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
141 BENCHMARK(BM_MemcasememMedium);
143 void BM_MemcasememPathological(benchmark::State& state) {
144 for (
auto _ : state) {
146 kHaystack, kHaystackSize, kHaystack + kHaystackSize / 2,
147 kHaystackSize - kHaystackSize / 2));
149 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
151 BENCHMARK(BM_MemcasememPathological);
153 bool case_eq(
const char a,
const char b) {
157 void BM_Search(benchmark::State& state) {
158 for (
auto _ : state) {
159 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
160 kHaystack + kHaystackSize - 1,
161 kHaystack + kHaystackSize));
163 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
165 BENCHMARK(BM_Search);
167 void BM_SearchMedium(benchmark::State& state) {
168 for (
auto _ : state) {
169 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
170 kHaystack + kHaystackSize - 2,
171 kHaystack + kHaystackSize));
173 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
175 BENCHMARK(BM_SearchMedium);
177 void BM_SearchPathological(benchmark::State& state) {
178 for (
auto _ : state) {
179 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
180 kHaystack + kHaystackSize / 2,
181 kHaystack + kHaystackSize));
183 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
185 BENCHMARK(BM_SearchPathological);
187 void BM_Searchcase(benchmark::State& state) {
188 for (
auto _ : state) {
189 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
190 kHaystack + kHaystackSize - 1,
191 kHaystack + kHaystackSize, case_eq));
193 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
195 BENCHMARK(BM_Searchcase);
197 void BM_SearchcaseMedium(benchmark::State& state) {
198 for (
auto _ : state) {
199 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
200 kHaystack + kHaystackSize - 2,
201 kHaystack + kHaystackSize, case_eq));
203 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
205 BENCHMARK(BM_SearchcaseMedium);
207 void BM_SearchcasePathological(benchmark::State& state) {
208 for (
auto _ : state) {
209 benchmark::DoNotOptimize(std::search(kHaystack, kHaystack + kHaystackSize,
210 kHaystack + kHaystackSize / 2,
211 kHaystack + kHaystackSize, case_eq));
213 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
215 BENCHMARK(BM_SearchcasePathological);
217 char* memcasechr(
const char* s,
int c,
size_t slen) {
219 for (; slen; ++s, --slen) {
225 const char* memcasematch(
const char* phaystack,
size_t haylen,
226 const char* pneedle,
size_t neelen) {
230 if (haylen < neelen)
return nullptr;
233 const char* hayend = phaystack + haylen - neelen + 1;
234 while ((match = static_cast<char*>(
235 memcasechr(phaystack, pneedle[0], hayend - phaystack)))) {
239 phaystack = match + 1;
244 void BM_Memmatch(benchmark::State& state) {
245 for (
auto _ : state) {
246 benchmark::DoNotOptimize(
249 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
251 BENCHMARK(BM_Memmatch);
253 void BM_MemmatchMedium(benchmark::State& state) {
254 for (
auto _ : state) {
255 benchmark::DoNotOptimize(
258 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
260 BENCHMARK(BM_MemmatchMedium);
262 void BM_MemmatchPathological(benchmark::State& state) {
263 for (
auto _ : state) {
265 kHaystack, kHaystackSize, kHaystack + kHaystackSize / 2,
266 kHaystackSize - kHaystackSize / 2));
268 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
270 BENCHMARK(BM_MemmatchPathological);
272 void BM_Memcasematch(benchmark::State& state) {
273 for (
auto _ : state) {
274 benchmark::DoNotOptimize(memcasematch(kHaystack, kHaystackSize,
"b", 1));
276 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
278 BENCHMARK(BM_Memcasematch);
280 void BM_MemcasematchMedium(benchmark::State& state) {
281 for (
auto _ : state) {
282 benchmark::DoNotOptimize(memcasematch(kHaystack, kHaystackSize,
"ab", 2));
284 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
286 BENCHMARK(BM_MemcasematchMedium);
288 void BM_MemcasematchPathological(benchmark::State& state) {
289 for (
auto _ : state) {
290 benchmark::DoNotOptimize(memcasematch(kHaystack, kHaystackSize,
291 kHaystack + kHaystackSize / 2,
292 kHaystackSize - kHaystackSize / 2));
294 state.SetBytesProcessed(kHaystackSize64 * state.iterations());
296 BENCHMARK(BM_MemcasematchPathological);
298 void BM_MemmemStartup(benchmark::State& state) {
299 for (
auto _ : state) {
301 kHaystack + kHaystackSize - 10, 10, kHaystack + kHaystackSize - 1, 1));
304 BENCHMARK(BM_MemmemStartup);
306 void BM_SearchStartup(benchmark::State& state) {
307 for (
auto _ : state) {
308 benchmark::DoNotOptimize(
309 std::search(kHaystack + kHaystackSize - 10, kHaystack + kHaystackSize,
310 kHaystack + kHaystackSize - 1, kHaystack + kHaystackSize));
313 BENCHMARK(BM_SearchStartup);
315 void BM_MemmatchStartup(benchmark::State& state) {
316 for (
auto _ : state) {
318 kHaystack + kHaystackSize - 10, 10, kHaystack + kHaystackSize - 1, 1));
321 BENCHMARK(BM_MemmatchStartup);
const char * memmem(const char *phaystack, size_t haylen, const char *pneedle, size_t needlelen)
const char * memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen)
char ascii_tolower(unsigned char c)
const char * memcasemem(const char *phaystack, size_t haylen, const char *pneedle, size_t needlelen)
int memcasecmp(const char *s1, const char *s2, size_t len)