Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 1 | #include <unordered_set> |
| 2 | #include <vector> |
| 3 | #include <cstdint> |
| 4 | |
Eric Fiselier | 87038cd | 2018-01-18 04:23:01 +0000 | [diff] [blame] | 5 | #include "benchmark/benchmark.h" |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 6 | #include "GenerateInput.hpp" |
| 7 | |
| 8 | constexpr std::size_t MAX_STRING_LEN = 8 << 14; |
| 9 | |
| 10 | // Benchmark when there is no match. |
| 11 | static void BM_StringFindNoMatch(benchmark::State &state) { |
| 12 | std::string s1(state.range(0), '-'); |
| 13 | std::string s2(8, '*'); |
| 14 | while (state.KeepRunning()) |
| 15 | benchmark::DoNotOptimize(s1.find(s2)); |
| 16 | } |
| 17 | BENCHMARK(BM_StringFindNoMatch)->Range(10, MAX_STRING_LEN); |
| 18 | |
| 19 | // Benchmark when the string matches first time. |
| 20 | static void BM_StringFindAllMatch(benchmark::State &state) { |
| 21 | std::string s1(MAX_STRING_LEN, '-'); |
| 22 | std::string s2(state.range(0), '-'); |
| 23 | while (state.KeepRunning()) |
| 24 | benchmark::DoNotOptimize(s1.find(s2)); |
| 25 | } |
| 26 | BENCHMARK(BM_StringFindAllMatch)->Range(1, MAX_STRING_LEN); |
| 27 | |
| 28 | // Benchmark when the string matches somewhere in the end. |
| 29 | static void BM_StringFindMatch1(benchmark::State &state) { |
| 30 | std::string s1(MAX_STRING_LEN / 2, '*'); |
| 31 | s1 += std::string(state.range(0), '-'); |
| 32 | std::string s2(state.range(0), '-'); |
| 33 | while (state.KeepRunning()) |
| 34 | benchmark::DoNotOptimize(s1.find(s2)); |
| 35 | } |
| 36 | BENCHMARK(BM_StringFindMatch1)->Range(1, MAX_STRING_LEN / 4); |
| 37 | |
| 38 | // Benchmark when the string matches somewhere from middle to the end. |
| 39 | static void BM_StringFindMatch2(benchmark::State &state) { |
| 40 | std::string s1(MAX_STRING_LEN / 2, '*'); |
| 41 | s1 += std::string(state.range(0), '-'); |
| 42 | s1 += std::string(state.range(0), '*'); |
| 43 | std::string s2(state.range(0), '-'); |
| 44 | while (state.KeepRunning()) |
| 45 | benchmark::DoNotOptimize(s1.find(s2)); |
| 46 | } |
| 47 | BENCHMARK(BM_StringFindMatch2)->Range(1, MAX_STRING_LEN / 4); |
| 48 | |
Eric Fiselier | 718a8ec | 2018-07-10 04:11:22 +0000 | [diff] [blame] | 49 | static void BM_StringCtorDefault(benchmark::State &state) { |
| 50 | while (state.KeepRunning()) { |
| 51 | for (unsigned I=0; I < 1000; ++I) { |
| 52 | std::string Default; |
| 53 | benchmark::DoNotOptimize(Default.c_str()); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | BENCHMARK(BM_StringCtorDefault); |
| 58 | |
| 59 | static void BM_StringCtorCStr(benchmark::State &state) { |
| 60 | std::string Input = getRandomString(state.range(0)); |
| 61 | const char *Str = Input.c_str(); |
| 62 | benchmark::DoNotOptimize(Str); |
| 63 | while (state.KeepRunning()) { |
| 64 | std::string Tmp(Str); |
| 65 | benchmark::DoNotOptimize(Tmp.c_str()); |
| 66 | } |
| 67 | } |
| 68 | BENCHMARK(BM_StringCtorCStr)->Arg(1)->Arg(8)->Range(16, MAX_STRING_LEN / 4); |
| 69 | |
Eric Fiselier | 87038cd | 2018-01-18 04:23:01 +0000 | [diff] [blame] | 70 | BENCHMARK_MAIN(); |