charconv_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/charconv.h"
16 
17 #include <cstdlib>
18 #include <cstring>
19 #include <string>
20 
21 #include "benchmark/benchmark.h"
22 
23 namespace {
24 
25 void BM_Strtod_Pi(benchmark::State& state) {
26  const char* pi = "3.14159";
27  for (auto s : state) {
28  benchmark::DoNotOptimize(pi);
29  benchmark::DoNotOptimize(strtod(pi, nullptr));
30  }
31 }
32 BENCHMARK(BM_Strtod_Pi);
33 
34 void BM_Absl_Pi(benchmark::State& state) {
35  const char* pi = "3.14159";
36  const char* pi_end = pi + strlen(pi);
37  for (auto s : state) {
38  benchmark::DoNotOptimize(pi);
39  double v;
40  absl::from_chars(pi, pi_end, v);
41  benchmark::DoNotOptimize(v);
42  }
43 }
44 BENCHMARK(BM_Absl_Pi);
45 
46 void BM_Strtod_Pi_float(benchmark::State& state) {
47  const char* pi = "3.14159";
48  for (auto s : state) {
49  benchmark::DoNotOptimize(pi);
50  benchmark::DoNotOptimize(strtof(pi, nullptr));
51  }
52 }
53 BENCHMARK(BM_Strtod_Pi_float);
54 
55 void BM_Absl_Pi_float(benchmark::State& state) {
56  const char* pi = "3.14159";
57  const char* pi_end = pi + strlen(pi);
58  for (auto s : state) {
59  benchmark::DoNotOptimize(pi);
60  float v;
61  absl::from_chars(pi, pi_end, v);
62  benchmark::DoNotOptimize(v);
63  }
64 }
65 BENCHMARK(BM_Absl_Pi_float);
66 
67 void BM_Strtod_HardLarge(benchmark::State& state) {
68  const char* num = "272104041512242479.e200";
69  for (auto s : state) {
70  benchmark::DoNotOptimize(num);
71  benchmark::DoNotOptimize(strtod(num, nullptr));
72  }
73 }
74 BENCHMARK(BM_Strtod_HardLarge);
75 
76 void BM_Absl_HardLarge(benchmark::State& state) {
77  const char* numstr = "272104041512242479.e200";
78  const char* numstr_end = numstr + strlen(numstr);
79  for (auto s : state) {
80  benchmark::DoNotOptimize(numstr);
81  double v;
82  absl::from_chars(numstr, numstr_end, v);
83  benchmark::DoNotOptimize(v);
84  }
85 }
86 BENCHMARK(BM_Absl_HardLarge);
87 
88 void BM_Strtod_HardSmall(benchmark::State& state) {
89  const char* num = "94080055902682397.e-242";
90  for (auto s : state) {
91  benchmark::DoNotOptimize(num);
92  benchmark::DoNotOptimize(strtod(num, nullptr));
93  }
94 }
95 BENCHMARK(BM_Strtod_HardSmall);
96 
97 void BM_Absl_HardSmall(benchmark::State& state) {
98  const char* numstr = "94080055902682397.e-242";
99  const char* numstr_end = numstr + strlen(numstr);
100  for (auto s : state) {
101  benchmark::DoNotOptimize(numstr);
102  double v;
103  absl::from_chars(numstr, numstr_end, v);
104  benchmark::DoNotOptimize(v);
105  }
106 }
107 BENCHMARK(BM_Absl_HardSmall);
108 
109 void BM_Strtod_HugeMantissa(benchmark::State& state) {
110  std::string huge(200, '3');
111  const char* num = huge.c_str();
112  for (auto s : state) {
113  benchmark::DoNotOptimize(num);
114  benchmark::DoNotOptimize(strtod(num, nullptr));
115  }
116 }
117 BENCHMARK(BM_Strtod_HugeMantissa);
118 
119 void BM_Absl_HugeMantissa(benchmark::State& state) {
120  std::string huge(200, '3');
121  const char* num = huge.c_str();
122  const char* num_end = num + 200;
123  for (auto s : state) {
124  benchmark::DoNotOptimize(num);
125  double v;
126  absl::from_chars(num, num_end, v);
127  benchmark::DoNotOptimize(v);
128  }
129 }
130 BENCHMARK(BM_Absl_HugeMantissa);
131 
132 std::string MakeHardCase(int length) {
133  // The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and
134  // the next larger representable number. The digits of this number are in
135  // the std::string below.
136  const std::string digits =
137  "1."
138  "152113937042223790993097181572444900347587985074226836242307364987727724"
139  "831384300183638649152607195040591791364113930628852279348613864894524591"
140  "272746490313676832900762939595690019745859128071117417798540258114233761"
141  "012939937017879509401007964861774960297319002612457273148497158989073482"
142  "171377406078223015359818300988676687994537274548940612510414856761641652"
143  "513434981938564294004070500716200446656421722229202383105446378511678258"
144  "370570631774499359748259931676320916632111681001853983492795053244971606"
145  "922718923011680846577744433974087653954904214152517799883551075537146316"
146  "168973685866425605046988661997658648354773076621610279716804960009043764"
147  "038392994055171112475093876476783502487512538082706095923790634572014823"
148  "78877699375152587890625" +
149  std::string(5000, '0');
150  // generate the hard cases on either side for the given length.
151  // Lengths between 3 and 1000 are reasonable.
152  return digits.substr(0, length) + "1e-297";
153 }
154 
155 void BM_Strtod_Big_And_Difficult(benchmark::State& state) {
156  std::string testcase = MakeHardCase(state.range(0));
157  const char* begin = testcase.c_str();
158  for (auto s : state) {
159  benchmark::DoNotOptimize(begin);
160  benchmark::DoNotOptimize(strtod(begin, nullptr));
161  }
162 }
163 BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
164 
165 void BM_Absl_Big_And_Difficult(benchmark::State& state) {
166  std::string testcase = MakeHardCase(state.range(0));
167  const char* begin = testcase.c_str();
168  const char* end = begin + testcase.size();
169  for (auto s : state) {
170  benchmark::DoNotOptimize(begin);
171  double v;
172  absl::from_chars(begin, end, v);
173  benchmark::DoNotOptimize(v);
174  }
175 }
176 BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
177 
178 } // namespace
179 
180 // ------------------------------------------------------------------------
181 // Benchmark Time CPU Iterations
182 // ------------------------------------------------------------------------
183 // BM_Strtod_Pi 96 ns 96 ns 6337454
184 // BM_Absl_Pi 35 ns 35 ns 20031996
185 // BM_Strtod_Pi_float 91 ns 91 ns 7745851
186 // BM_Absl_Pi_float 35 ns 35 ns 20430298
187 // BM_Strtod_HardLarge 133 ns 133 ns 5288341
188 // BM_Absl_HardLarge 181 ns 181 ns 3855615
189 // BM_Strtod_HardSmall 279 ns 279 ns 2517243
190 // BM_Absl_HardSmall 287 ns 287 ns 2458744
191 // BM_Strtod_HugeMantissa 433 ns 433 ns 1604293
192 // BM_Absl_HugeMantissa 160 ns 160 ns 4403671
193 // BM_Strtod_Big_And_Difficult/3 236 ns 236 ns 2942496
194 // BM_Strtod_Big_And_Difficult/8 232 ns 232 ns 2983796
195 // BM_Strtod_Big_And_Difficult/64 437 ns 437 ns 1591951
196 // BM_Strtod_Big_And_Difficult/512 1738 ns 1738 ns 402519
197 // BM_Strtod_Big_And_Difficult/4096 3943 ns 3943 ns 176128
198 // BM_Strtod_Big_And_Difficult/5000 4397 ns 4397 ns 157878
199 // BM_Absl_Big_And_Difficult/3 39 ns 39 ns 17799583
200 // BM_Absl_Big_And_Difficult/8 43 ns 43 ns 16096859
201 // BM_Absl_Big_And_Difficult/64 550 ns 550 ns 1259717
202 // BM_Absl_Big_And_Difficult/512 4167 ns 4167 ns 171414
203 // BM_Absl_Big_And_Difficult/4096 9160 ns 9159 ns 76297
204 // BM_Absl_Big_And_Difficult/5000 9738 ns 9738 ns 70140
int v
Definition: variant_test.cc:81
char * begin
char * end
from_chars_result from_chars(const char *first, const char *last, double &value, chars_format fmt)
Definition: charconv.cc:679
std::size_t length
Definition: test_util.cc:52


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35