1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdint.h>
18 #include <string.h>
19
20 #include <benchmark/benchmark.h>
21
22 constexpr auto KB = 1024;
23
24 #define AT_COMMON_SIZES \
25 Arg(8)->Arg(64)->Arg(512)->Arg(1*KB)->Arg(8*KB)->Arg(16*KB)->Arg(32*KB)->Arg(64*KB)
26
27 // TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.)
28
BM_string_memcmp(benchmark::State & state)29 static void BM_string_memcmp(benchmark::State& state) {
30 const size_t nbytes = state.range(0);
31 char* src = new char[nbytes]; char* dst = new char[nbytes];
32 memset(src, 'x', nbytes);
33 memset(dst, 'x', nbytes);
34
35 volatile int c __attribute__((unused)) = 0;
36 while (state.KeepRunning()) {
37 c += memcmp(dst, src, nbytes);
38 }
39
40 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
41 delete[] src;
42 delete[] dst;
43 }
44 BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES;
45
BM_string_memcpy(benchmark::State & state)46 static void BM_string_memcpy(benchmark::State& state) {
47 const size_t nbytes = state.range(0);
48 char* src = new char[nbytes]; char* dst = new char[nbytes];
49 memset(src, 'x', nbytes);
50
51 while (state.KeepRunning()) {
52 memcpy(dst, src, nbytes);
53 }
54
55 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
56 delete[] src;
57 delete[] dst;
58 }
59 BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES;
60
BM_string_memmove(benchmark::State & state)61 static void BM_string_memmove(benchmark::State& state) {
62 const size_t nbytes = state.range(0);
63 char* buf = new char[nbytes + 64];
64 memset(buf, 'x', nbytes + 64);
65
66 while (state.KeepRunning()) {
67 memmove(buf, buf + 1, nbytes); // Worst-case overlap.
68 }
69
70 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
71 delete[] buf;
72 }
73 BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES;
74
BM_string_memset(benchmark::State & state)75 static void BM_string_memset(benchmark::State& state) {
76 const size_t nbytes = state.range(0);
77 char* dst = new char[nbytes];
78
79 while (state.KeepRunning()) {
80 memset(dst, 0, nbytes);
81 }
82
83 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
84 delete[] dst;
85 }
86 BENCHMARK(BM_string_memset)->AT_COMMON_SIZES;
87
BM_string_strlen(benchmark::State & state)88 static void BM_string_strlen(benchmark::State& state) {
89 const size_t nbytes = state.range(0);
90 char* s = new char[nbytes];
91 memset(s, 'x', nbytes);
92 s[nbytes - 1] = 0;
93
94 volatile int c __attribute__((unused)) = 0;
95 while (state.KeepRunning()) {
96 c += strlen(s);
97 }
98
99 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
100 delete[] s;
101 }
102 BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES;
103