diff options
Diffstat (limited to 'benchmarks/string_benchmark.cpp')
-rw-r--r-- | benchmarks/string_benchmark.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/benchmarks/string_benchmark.cpp b/benchmarks/string_benchmark.cpp index aa1e8c9aa..e6c65703e 100644 --- a/benchmarks/string_benchmark.cpp +++ b/benchmarks/string_benchmark.cpp @@ -123,3 +123,53 @@ static void BM_string_strlen(benchmark::State& state) { delete[] s; } BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; + +static void BM_string_strcat_copy_only(benchmark::State& state) { + const size_t nbytes = state.range(0); + std::vector<char> src(nbytes, 'x'); + std::vector<char> dst(nbytes + 2); + src[nbytes - 1] = '\0'; + dst[0] = 'y'; + dst[1] = 'y'; + dst[2] = '\0'; + + while (state.KeepRunning()) { + strcat(dst.data(), src.data()); + dst[2] = '\0'; + } + + state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); +} +BENCHMARK(BM_string_strcat_copy_only)->AT_COMMON_SIZES; + +static void BM_string_strcat_seek_only(benchmark::State& state) { + const size_t nbytes = state.range(0); + std::vector<char> src(3, 'x'); + std::vector<char> dst(nbytes + 2, 'y'); + src[2] = '\0'; + dst[nbytes - 1] = '\0'; + + while (state.KeepRunning()) { + strcat(dst.data(), src.data()); + dst[nbytes - 1] = '\0'; + } + + state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); +} +BENCHMARK(BM_string_strcat_seek_only)->AT_COMMON_SIZES; + +static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) { + const size_t nbytes = state.range(0); + std::vector<char> src(nbytes / 2, 'x'); + std::vector<char> dst(nbytes / 2, 'y'); + src[nbytes / 2 - 1] = '\0'; + dst[nbytes / 2 - 1] = '\0'; + + while (state.KeepRunning()) { + strcat(dst.data(), src.data()); + dst[nbytes / 2 - 1] = '\0'; + } + + state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); +} +BENCHMARK(BM_string_strcat_half_copy_half_seek)->AT_COMMON_SIZES; |