From a99d052801be5618465e5532440711e61f9b89fc Mon Sep 17 00:00:00 2001 From: Anders Lewis Date: Mon, 12 Jun 2017 11:24:01 -0700 Subject: [PATCH] Add copy-only, seek-only, and copy-and-seek benchmarks for strcat Test: Ran benchmarks and verified that runtimes were sensible. Change-Id: I6da3aaea0ba4817662534b24873993589385e6c5 --- benchmarks/string_benchmark.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) 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 src(nbytes, 'x'); + std::vector 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 src(3, 'x'); + std::vector 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 src(nbytes / 2, 'x'); + std::vector 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; -- 2.11.0