Add copy-only, seek-only, and copy-and-seek benchmarks for strcat
Test: Ran benchmarks and verified that runtimes were sensible.
Change-Id: I6da3aaea0ba4817662534b24873993589385e6c5
diff --git a/benchmarks/string_benchmark.cpp b/benchmarks/string_benchmark.cpp
index aa1e8c9..e6c6570 100644
--- a/benchmarks/string_benchmark.cpp
+++ b/benchmarks/string_benchmark.cpp
@@ -123,3 +123,53 @@
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;