OSDN Git Service

Add copy-only, seek-only, and copy-and-seek benchmarks for strcat
authorAnders Lewis <agloo@google.com>
Mon, 12 Jun 2017 18:24:01 +0000 (11:24 -0700)
committerAnders Lewis <agloo@google.com>
Wed, 14 Jun 2017 22:13:50 +0000 (15:13 -0700)
Test: Ran benchmarks and verified that runtimes were sensible.
Change-Id: I6da3aaea0ba4817662534b24873993589385e6c5

benchmarks/string_benchmark.cpp

index aa1e8c9..e6c6570 100644 (file)
@@ -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;