Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 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 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 17 | #include <stdint.h> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 20 | #include <benchmark/benchmark.h> |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 21 | |
Chih-Hung Hsieh | 1a5fd9c | 2016-06-10 11:07:21 -0700 | [diff] [blame] | 22 | constexpr auto KB = 1024; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 29 | static void BM_string_memcmp(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 30 | const size_t nbytes = state.range(0); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 31 | char* src = new char[nbytes]; char* dst = new char[nbytes]; |
| 32 | memset(src, 'x', nbytes); |
| 33 | memset(dst, 'x', nbytes); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 34 | |
| 35 | volatile int c __attribute__((unused)) = 0; |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 36 | while (state.KeepRunning()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 37 | c += memcmp(dst, src, nbytes); |
| 38 | } |
| 39 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 40 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 41 | delete[] src; |
| 42 | delete[] dst; |
| 43 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 44 | BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 46 | static void BM_string_memcpy(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 47 | const size_t nbytes = state.range(0); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 48 | char* src = new char[nbytes]; char* dst = new char[nbytes]; |
| 49 | memset(src, 'x', nbytes); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 51 | while (state.KeepRunning()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 52 | memcpy(dst, src, nbytes); |
| 53 | } |
| 54 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 55 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 56 | delete[] src; |
| 57 | delete[] dst; |
| 58 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 59 | BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 60 | |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 61 | static void BM_string_memmove_non_overlapping(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 62 | const size_t nbytes = state.range(0); |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 63 | std::vector<char> src(nbytes, 'x'); |
| 64 | std::vector<char> dst(nbytes, 'x'); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 65 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 66 | while (state.KeepRunning()) { |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 67 | memmove(dst.data(), src.data(), nbytes); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 70 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 71 | } |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 72 | BENCHMARK(BM_string_memmove_non_overlapping)->AT_COMMON_SIZES; |
| 73 | |
| 74 | static void BM_string_memmove_overlap_dst_before_src(benchmark::State& state) { |
| 75 | const size_t nbytes = state.range(0); |
| 76 | std::vector<char> buf(nbytes + 1, 'x'); |
| 77 | |
| 78 | while (state.KeepRunning()) { |
| 79 | memmove(buf.data(), buf.data() + 1, nbytes); // Worst-case overlap. |
| 80 | } |
| 81 | |
| 82 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 83 | } |
| 84 | BENCHMARK(BM_string_memmove_overlap_dst_before_src)->AT_COMMON_SIZES; |
| 85 | |
| 86 | static void BM_string_memmove_overlap_src_before_dst(benchmark::State& state) { |
| 87 | const size_t nbytes = state.range(0); |
| 88 | std::vector<char> buf(nbytes + 1, 'x'); |
| 89 | |
| 90 | while (state.KeepRunning()) { |
| 91 | memmove(buf.data() + 1, buf.data(), nbytes); // Worst-case overlap. |
| 92 | } |
| 93 | |
| 94 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 95 | } |
| 96 | BENCHMARK(BM_string_memmove_overlap_src_before_dst)->AT_COMMON_SIZES; |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 98 | static void BM_string_memset(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 99 | const size_t nbytes = state.range(0); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 100 | char* dst = new char[nbytes]; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 101 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 102 | while (state.KeepRunning()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 103 | memset(dst, 0, nbytes); |
| 104 | } |
| 105 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 106 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 107 | delete[] dst; |
| 108 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 109 | BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 110 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 111 | static void BM_string_strlen(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 112 | const size_t nbytes = state.range(0); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 113 | char* s = new char[nbytes]; |
| 114 | memset(s, 'x', nbytes); |
| 115 | s[nbytes - 1] = 0; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 116 | |
| 117 | volatile int c __attribute__((unused)) = 0; |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 118 | while (state.KeepRunning()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 119 | c += strlen(s); |
| 120 | } |
| 121 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 122 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 123 | delete[] s; |
| 124 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 125 | BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 126 | |
| 127 | static void BM_string_strcat_copy_only(benchmark::State& state) { |
| 128 | const size_t nbytes = state.range(0); |
| 129 | std::vector<char> src(nbytes, 'x'); |
| 130 | std::vector<char> dst(nbytes + 2); |
| 131 | src[nbytes - 1] = '\0'; |
| 132 | dst[0] = 'y'; |
| 133 | dst[1] = 'y'; |
| 134 | dst[2] = '\0'; |
| 135 | |
| 136 | while (state.KeepRunning()) { |
| 137 | strcat(dst.data(), src.data()); |
| 138 | dst[2] = '\0'; |
| 139 | } |
| 140 | |
| 141 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 142 | } |
| 143 | BENCHMARK(BM_string_strcat_copy_only)->AT_COMMON_SIZES; |
| 144 | |
| 145 | static void BM_string_strcat_seek_only(benchmark::State& state) { |
| 146 | const size_t nbytes = state.range(0); |
| 147 | std::vector<char> src(3, 'x'); |
| 148 | std::vector<char> dst(nbytes + 2, 'y'); |
| 149 | src[2] = '\0'; |
| 150 | dst[nbytes - 1] = '\0'; |
| 151 | |
| 152 | while (state.KeepRunning()) { |
| 153 | strcat(dst.data(), src.data()); |
| 154 | dst[nbytes - 1] = '\0'; |
| 155 | } |
| 156 | |
| 157 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 158 | } |
| 159 | BENCHMARK(BM_string_strcat_seek_only)->AT_COMMON_SIZES; |
| 160 | |
| 161 | static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) { |
| 162 | const size_t nbytes = state.range(0); |
| 163 | std::vector<char> src(nbytes / 2, 'x'); |
| 164 | std::vector<char> dst(nbytes / 2, 'y'); |
| 165 | src[nbytes / 2 - 1] = '\0'; |
| 166 | dst[nbytes / 2 - 1] = '\0'; |
| 167 | |
| 168 | while (state.KeepRunning()) { |
| 169 | strcat(dst.data(), src.data()); |
| 170 | dst[nbytes / 2 - 1] = '\0'; |
| 171 | } |
| 172 | |
| 173 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 174 | } |
| 175 | BENCHMARK(BM_string_strcat_half_copy_half_seek)->AT_COMMON_SIZES; |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame^] | 176 | |
| 177 | static void BM_string_strcpy(benchmark::State& state) { |
| 178 | const size_t nbytes = state.range(0); |
| 179 | std::vector<char> src(nbytes, 'x'); |
| 180 | std::vector<char> dst(nbytes); |
| 181 | src[nbytes - 1] = '\0'; |
| 182 | |
| 183 | while (state.KeepRunning()) { |
| 184 | strcpy(dst.data(), src.data()); |
| 185 | } |
| 186 | |
| 187 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 188 | } |
| 189 | BENCHMARK(BM_string_strcpy)->AT_COMMON_SIZES; |
| 190 | |
| 191 | static void BM_string_strcmp(benchmark::State& state) { |
| 192 | const size_t nbytes = state.range(0); |
| 193 | std::vector<char> s1(nbytes, 'x'); |
| 194 | std::vector<char> s2(nbytes, 'x'); |
| 195 | s1[nbytes - 1] = '\0'; |
| 196 | s2[nbytes - 1] = '\0'; |
| 197 | |
| 198 | volatile int c __attribute__((unused)); |
| 199 | while (state.KeepRunning()) { |
| 200 | c = strcmp(s1.data(), s2.data()); |
| 201 | } |
| 202 | |
| 203 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 204 | } |
| 205 | BENCHMARK(BM_string_strcmp)->AT_COMMON_SIZES; |