2016-12-31 02:01:36 +08:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2018-01-18 12:23:01 +08:00
|
|
|
#include "benchmark/benchmark.h"
|
2016-12-31 02:01:36 +08:00
|
|
|
#include "GenerateInput.hpp"
|
|
|
|
|
|
|
|
constexpr std::size_t MAX_STRING_LEN = 8 << 14;
|
|
|
|
|
|
|
|
// Benchmark when there is no match.
|
|
|
|
static void BM_StringFindNoMatch(benchmark::State &state) {
|
|
|
|
std::string s1(state.range(0), '-');
|
|
|
|
std::string s2(8, '*');
|
|
|
|
while (state.KeepRunning())
|
|
|
|
benchmark::DoNotOptimize(s1.find(s2));
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_StringFindNoMatch)->Range(10, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
// Benchmark when the string matches first time.
|
|
|
|
static void BM_StringFindAllMatch(benchmark::State &state) {
|
|
|
|
std::string s1(MAX_STRING_LEN, '-');
|
|
|
|
std::string s2(state.range(0), '-');
|
|
|
|
while (state.KeepRunning())
|
|
|
|
benchmark::DoNotOptimize(s1.find(s2));
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_StringFindAllMatch)->Range(1, MAX_STRING_LEN);
|
|
|
|
|
|
|
|
// Benchmark when the string matches somewhere in the end.
|
|
|
|
static void BM_StringFindMatch1(benchmark::State &state) {
|
|
|
|
std::string s1(MAX_STRING_LEN / 2, '*');
|
|
|
|
s1 += std::string(state.range(0), '-');
|
|
|
|
std::string s2(state.range(0), '-');
|
|
|
|
while (state.KeepRunning())
|
|
|
|
benchmark::DoNotOptimize(s1.find(s2));
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_StringFindMatch1)->Range(1, MAX_STRING_LEN / 4);
|
|
|
|
|
|
|
|
// Benchmark when the string matches somewhere from middle to the end.
|
|
|
|
static void BM_StringFindMatch2(benchmark::State &state) {
|
|
|
|
std::string s1(MAX_STRING_LEN / 2, '*');
|
|
|
|
s1 += std::string(state.range(0), '-');
|
|
|
|
s1 += std::string(state.range(0), '*');
|
|
|
|
std::string s2(state.range(0), '-');
|
|
|
|
while (state.KeepRunning())
|
|
|
|
benchmark::DoNotOptimize(s1.find(s2));
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_StringFindMatch2)->Range(1, MAX_STRING_LEN / 4);
|
|
|
|
|
2018-01-18 12:23:01 +08:00
|
|
|
BENCHMARK_MAIN();
|