charconv_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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   // The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and
00134   // the next larger representable number.  The digits of this number are in
00135   // the std::string below.
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   // generate the hard cases on either side for the given length.
00151   // Lengths between 3 and 1000 are reasonable.
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 }  // namespace
00179 
00180 // ------------------------------------------------------------------------
00181 // Benchmark                                 Time           CPU Iterations
00182 // ------------------------------------------------------------------------
00183 // BM_Strtod_Pi                             96 ns         96 ns    6337454
00184 // BM_Absl_Pi                               35 ns         35 ns   20031996
00185 // BM_Strtod_Pi_float                       91 ns         91 ns    7745851
00186 // BM_Absl_Pi_float                         35 ns         35 ns   20430298
00187 // BM_Strtod_HardLarge                     133 ns        133 ns    5288341
00188 // BM_Absl_HardLarge                       181 ns        181 ns    3855615
00189 // BM_Strtod_HardSmall                     279 ns        279 ns    2517243
00190 // BM_Absl_HardSmall                       287 ns        287 ns    2458744
00191 // BM_Strtod_HugeMantissa                  433 ns        433 ns    1604293
00192 // BM_Absl_HugeMantissa                    160 ns        160 ns    4403671
00193 // BM_Strtod_Big_And_Difficult/3           236 ns        236 ns    2942496
00194 // BM_Strtod_Big_And_Difficult/8           232 ns        232 ns    2983796
00195 // BM_Strtod_Big_And_Difficult/64          437 ns        437 ns    1591951
00196 // BM_Strtod_Big_And_Difficult/512        1738 ns       1738 ns     402519
00197 // BM_Strtod_Big_And_Difficult/4096       3943 ns       3943 ns     176128
00198 // BM_Strtod_Big_And_Difficult/5000       4397 ns       4397 ns     157878
00199 // BM_Absl_Big_And_Difficult/3              39 ns         39 ns   17799583
00200 // BM_Absl_Big_And_Difficult/8              43 ns         43 ns   16096859
00201 // BM_Absl_Big_And_Difficult/64            550 ns        550 ns    1259717
00202 // BM_Absl_Big_And_Difficult/512          4167 ns       4167 ns     171414
00203 // BM_Absl_Big_And_Difficult/4096         9160 ns       9159 ns      76297
00204 // BM_Absl_Big_And_Difficult/5000         9738 ns       9738 ns      70140


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14