21 #include "benchmark/benchmark.h" 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));
32 BENCHMARK(BM_Strtod_Pi);
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);
41 benchmark::DoNotOptimize(v);
44 BENCHMARK(BM_Absl_Pi);
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));
53 BENCHMARK(BM_Strtod_Pi_float);
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);
62 benchmark::DoNotOptimize(v);
65 BENCHMARK(BM_Absl_Pi_float);
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));
74 BENCHMARK(BM_Strtod_HardLarge);
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);
83 benchmark::DoNotOptimize(v);
86 BENCHMARK(BM_Absl_HardLarge);
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));
95 BENCHMARK(BM_Strtod_HardSmall);
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);
104 benchmark::DoNotOptimize(v);
107 BENCHMARK(BM_Absl_HardSmall);
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));
117 BENCHMARK(BM_Strtod_HugeMantissa);
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);
127 benchmark::DoNotOptimize(v);
130 BENCHMARK(BM_Absl_HugeMantissa);
132 std::string MakeHardCase(
int length) {
136 const std::string digits =
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');
152 return digits.substr(0, length) +
"1e-297";
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));
163 BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
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);
173 benchmark::DoNotOptimize(v);
176 BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
from_chars_result from_chars(const char *first, const char *last, double &value, chars_format fmt)