Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/charconv.h"
00016
00017 #include <cstdlib>
00018 #include <cstring>
00019 #include <string>
00020
00021 #include "benchmark/benchmark.h"
00022
00023 namespace {
00024
00025 void BM_Strtod_Pi(benchmark::State& state) {
00026 const char* pi = "3.14159";
00027 for (auto s : state) {
00028 benchmark::DoNotOptimize(pi);
00029 benchmark::DoNotOptimize(strtod(pi, nullptr));
00030 }
00031 }
00032 BENCHMARK(BM_Strtod_Pi);
00033
00034 void BM_Absl_Pi(benchmark::State& state) {
00035 const char* pi = "3.14159";
00036 const char* pi_end = pi + strlen(pi);
00037 for (auto s : state) {
00038 benchmark::DoNotOptimize(pi);
00039 double v;
00040 absl::from_chars(pi, pi_end, v);
00041 benchmark::DoNotOptimize(v);
00042 }
00043 }
00044 BENCHMARK(BM_Absl_Pi);
00045
00046 void BM_Strtod_Pi_float(benchmark::State& state) {
00047 const char* pi = "3.14159";
00048 for (auto s : state) {
00049 benchmark::DoNotOptimize(pi);
00050 benchmark::DoNotOptimize(strtof(pi, nullptr));
00051 }
00052 }
00053 BENCHMARK(BM_Strtod_Pi_float);
00054
00055 void BM_Absl_Pi_float(benchmark::State& state) {
00056 const char* pi = "3.14159";
00057 const char* pi_end = pi + strlen(pi);
00058 for (auto s : state) {
00059 benchmark::DoNotOptimize(pi);
00060 float v;
00061 absl::from_chars(pi, pi_end, v);
00062 benchmark::DoNotOptimize(v);
00063 }
00064 }
00065 BENCHMARK(BM_Absl_Pi_float);
00066
00067 void BM_Strtod_HardLarge(benchmark::State& state) {
00068 const char* num = "272104041512242479.e200";
00069 for (auto s : state) {
00070 benchmark::DoNotOptimize(num);
00071 benchmark::DoNotOptimize(strtod(num, nullptr));
00072 }
00073 }
00074 BENCHMARK(BM_Strtod_HardLarge);
00075
00076 void BM_Absl_HardLarge(benchmark::State& state) {
00077 const char* numstr = "272104041512242479.e200";
00078 const char* numstr_end = numstr + strlen(numstr);
00079 for (auto s : state) {
00080 benchmark::DoNotOptimize(numstr);
00081 double v;
00082 absl::from_chars(numstr, numstr_end, v);
00083 benchmark::DoNotOptimize(v);
00084 }
00085 }
00086 BENCHMARK(BM_Absl_HardLarge);
00087
00088 void BM_Strtod_HardSmall(benchmark::State& state) {
00089 const char* num = "94080055902682397.e-242";
00090 for (auto s : state) {
00091 benchmark::DoNotOptimize(num);
00092 benchmark::DoNotOptimize(strtod(num, nullptr));
00093 }
00094 }
00095 BENCHMARK(BM_Strtod_HardSmall);
00096
00097 void BM_Absl_HardSmall(benchmark::State& state) {
00098 const char* numstr = "94080055902682397.e-242";
00099 const char* numstr_end = numstr + strlen(numstr);
00100 for (auto s : state) {
00101 benchmark::DoNotOptimize(numstr);
00102 double v;
00103 absl::from_chars(numstr, numstr_end, v);
00104 benchmark::DoNotOptimize(v);
00105 }
00106 }
00107 BENCHMARK(BM_Absl_HardSmall);
00108
00109 void BM_Strtod_HugeMantissa(benchmark::State& state) {
00110 std::string huge(200, '3');
00111 const char* num = huge.c_str();
00112 for (auto s : state) {
00113 benchmark::DoNotOptimize(num);
00114 benchmark::DoNotOptimize(strtod(num, nullptr));
00115 }
00116 }
00117 BENCHMARK(BM_Strtod_HugeMantissa);
00118
00119 void BM_Absl_HugeMantissa(benchmark::State& state) {
00120 std::string huge(200, '3');
00121 const char* num = huge.c_str();
00122 const char* num_end = num + 200;
00123 for (auto s : state) {
00124 benchmark::DoNotOptimize(num);
00125 double v;
00126 absl::from_chars(num, num_end, v);
00127 benchmark::DoNotOptimize(v);
00128 }
00129 }
00130 BENCHMARK(BM_Absl_HugeMantissa);
00131
00132 std::string MakeHardCase(int length) {
00133
00134
00135
00136 const std::string digits =
00137 "1."
00138 "152113937042223790993097181572444900347587985074226836242307364987727724"
00139 "831384300183638649152607195040591791364113930628852279348613864894524591"
00140 "272746490313676832900762939595690019745859128071117417798540258114233761"
00141 "012939937017879509401007964861774960297319002612457273148497158989073482"
00142 "171377406078223015359818300988676687994537274548940612510414856761641652"
00143 "513434981938564294004070500716200446656421722229202383105446378511678258"
00144 "370570631774499359748259931676320916632111681001853983492795053244971606"
00145 "922718923011680846577744433974087653954904214152517799883551075537146316"
00146 "168973685866425605046988661997658648354773076621610279716804960009043764"
00147 "038392994055171112475093876476783502487512538082706095923790634572014823"
00148 "78877699375152587890625" +
00149 std::string(5000, '0');
00150
00151
00152 return digits.substr(0, length) + "1e-297";
00153 }
00154
00155 void BM_Strtod_Big_And_Difficult(benchmark::State& state) {
00156 std::string testcase = MakeHardCase(state.range(0));
00157 const char* begin = testcase.c_str();
00158 for (auto s : state) {
00159 benchmark::DoNotOptimize(begin);
00160 benchmark::DoNotOptimize(strtod(begin, nullptr));
00161 }
00162 }
00163 BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
00164
00165 void BM_Absl_Big_And_Difficult(benchmark::State& state) {
00166 std::string testcase = MakeHardCase(state.range(0));
00167 const char* begin = testcase.c_str();
00168 const char* end = begin + testcase.size();
00169 for (auto s : state) {
00170 benchmark::DoNotOptimize(begin);
00171 double v;
00172 absl::from_chars(begin, end, v);
00173 benchmark::DoNotOptimize(v);
00174 }
00175 }
00176 BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
00177
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204