2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This file is supposed to be included from within
19 * FBStringTest. Do not use otherwise.
21 * override-include-guard
24 void BENCHFUN(initRNG)(size_t iters, size_t) {
27 BENCHMARK_PARAM(BENCHFUN(initRNG), 0);
29 void BENCHFUN(defaultCtor)(size_t iters, size_t) {
30 FOR_EACH_RANGE (i, 0, iters) {
32 doNotOptimizeAway(&s);
35 BENCHMARK_PARAM(BENCHFUN(defaultCtor), 0);
37 void BENCHFUN(copyCtor)(size_t iters, size_t arg) {
40 randomString(&s, arg);
42 FOR_EACH_RANGE (i, 0, iters) {
44 doNotOptimizeAway(&s1);
47 BENCHMARK_PARAM(BENCHFUN(copyCtor), 32768);
49 void BENCHFUN(ctorFromArray)(size_t iters, size_t arg) {
52 randomString(&s, arg);
57 FOR_EACH_RANGE (i, 0, iters) {
58 STRING s1(s.data(), s.size());
59 doNotOptimizeAway(&s1);
62 BENCHMARK_PARAM(BENCHFUN(ctorFromArray), 32768);
64 void BENCHFUN(ctorFromTwoPointers)(size_t iters, size_t arg) {
67 if (s.size() < arg) s.resize(arg);
69 FOR_EACH_RANGE (i, 0, iters) {
70 STRING s1(s.begin(), s.end());
71 doNotOptimizeAway(&s1);
74 BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 0);
75 BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 7);
76 BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 15);
77 BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 23);
78 BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 24);
80 void BENCHFUN(ctorFromChar)(size_t iters, size_t arg) {
81 FOR_EACH_RANGE (i, 0, iters) {
83 doNotOptimizeAway(&s1);
86 BENCHMARK_PARAM(BENCHFUN(ctorFromChar), 1048576);
88 void BENCHFUN(assignmentOp)(size_t iters, size_t arg) {
91 randomString(&s, arg);
93 FOR_EACH_RANGE (i, 0, iters) {
96 randomString(&s1, arg);
97 doNotOptimizeAway(&s1);
102 BENCHMARK_PARAM(BENCHFUN(assignmentOp), 256);
104 void BENCHFUN(assignmentFill)(size_t iters, size_t) {
106 FOR_EACH_RANGE (i, 0, iters) {
107 s = static_cast<char>(i);
108 doNotOptimizeAway(&s);
111 BENCHMARK_PARAM(BENCHFUN(assignmentFill), 0);
113 void BENCHFUN(resize)(size_t iters, size_t arg) {
115 FOR_EACH_RANGE (i, 0, iters) {
116 s.resize(random(0, arg));
117 doNotOptimizeAway(&s);
120 BENCHMARK_PARAM(BENCHFUN(resize), 524288);
122 void BENCHFUN(findSuccessful)(size_t iters, size_t arg) {
128 // Text courtesy (ahem) of
129 // http://www.psychologytoday.com/blog/career-transitions/200906/
130 // the-dreaded-writing-sample
132 Even if you've mastered the art of the cover letter and the resume, \
133 another part of the job search process can trip up an otherwise \
134 qualified candidate: the writing sample.\n\
136 Strong writing and communication skills are highly sought after by \
137 most employers. Whether crafting short emails or lengthy annual \
138 reports, many workers use their writing skills every day. And for an \
139 employer seeking proof behind that ubiquitous candidate \
140 phrase,\"excellent communication skills\", a required writing sample \
143 Writing samples need the same care and attention given to cover \
144 letters and resumes. Candidates with otherwise impeccable credentials \
145 are routinely eliminated by a poorly chosen writing sample. Notice I \
146 said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
147 a writing sample not only reveals the individual's writing skills, it \
148 also offers a peek into what they consider important or relevant for \
149 the position. If you miss that mark with your writing sample, don't \
150 expect to get a call for an interview.";
152 pos = random(0, s.size());
153 len = random(0, s.size() - pos);
155 FOR_EACH_RANGE (i, 0, iters) {
156 doNotOptimizeAway(s.find(s.data(), pos, len));
159 BENCHMARK_PARAM(BENCHFUN(findSuccessful), 524288);
161 void BENCHFUN(findUnsuccessful)(size_t iters, size_t arg) {
166 Even if you've mastered the art of the cover letter and the resume, \
167 another part of the job search process can trip up an otherwise \
168 qualified candidate: the writing sample.\n\
170 Strong writing and communication skills are highly sought after by \
171 most employers. Whether crafting short emails or lengthy annual \
172 reports, many workers use their writing skills every day. And for an \
173 employer seeking proof behind that ubiquitous candidate \
174 phrase,\"excellent communication skills\", a required writing sample \
177 Writing samples need the same care and attention given to cover \
178 letters and resumes. Candidates with otherwise impeccable credentials \
179 are routinely eliminated by a poorly chosen writing sample. Notice I \
180 said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
181 a writing sample not only reveals the individual's writing skills, it \
182 also offers a peek into what they consider important or relevant for \
183 the position. If you miss that mark with your writing sample, don't \
184 expect to get a call for an interview.";
186 s1 = "So how do you tackle that writing sample request?";
189 FOR_EACH_RANGE (i, 0, iters) {
190 doNotOptimizeAway(s.find(s1));
193 BENCHMARK_PARAM(BENCHFUN(findUnsuccessful), 524288);
195 void BENCHFUN(equality)(size_t iters, size_t arg) {
196 std::vector<STRING> haystack(arg);
199 for (auto& hay : haystack) {
200 randomBinaryString(&hay, 1024);
204 FOR_EACH_RANGE (i, 0, iters) {
206 randomBinaryString(&needle, 1024);
207 doNotOptimizeAway(std::find(haystack.begin(), haystack.end(), needle));
210 BENCHMARK_PARAM(BENCHFUN(equality), 65536);
212 void BENCHFUN(replace)(size_t iters, size_t arg) {
215 randomString(&s, arg);
217 FOR_EACH_RANGE (i, 0, iters) {
218 BenchmarkSuspender susp;
219 doNotOptimizeAway(&s);
220 auto const pos = random(0, s.size());
221 auto toRemove = random(0, s.size() - pos);
222 auto toInsert = random(0, arg);
224 randomString(&s1, toInsert);
227 s.replace(pos, toRemove, s1);
230 BENCHMARK_PARAM(BENCHFUN(replace), 256);
232 void BENCHFUN(push_back)(size_t iters, size_t arg) {
233 FOR_EACH_RANGE (i, 0, iters) {
235 FOR_EACH_RANGE (j, 0, arg) {
240 BENCHMARK_PARAM(BENCHFUN(push_back), 1);
241 BENCHMARK_PARAM(BENCHFUN(push_back), 23);
242 BENCHMARK_PARAM(BENCHFUN(push_back), 127);
243 BENCHMARK_PARAM(BENCHFUN(push_back), 1024);
245 void BENCHFUN(short_append)(size_t iters, size_t arg) {
246 FOR_EACH_RANGE (i, 0, iters) {
248 FOR_EACH_RANGE (j, 0, arg) {
253 BENCHMARK_PARAM(BENCHFUN(short_append), 23);
254 BENCHMARK_PARAM(BENCHFUN(short_append), 1024);