charconv_benchmark.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "absl/strings/charconv.h"
  15. #include <cstdlib>
  16. #include <cstring>
  17. #include <string>
  18. #include "benchmark/benchmark.h"
  19. namespace {
  20. void BM_Strtod_Pi(benchmark::State& state) {
  21. const char* pi = "3.14159";
  22. for (auto s : state) {
  23. benchmark::DoNotOptimize(pi);
  24. benchmark::DoNotOptimize(strtod(pi, nullptr));
  25. }
  26. }
  27. BENCHMARK(BM_Strtod_Pi);
  28. void BM_Absl_Pi(benchmark::State& state) {
  29. const char* pi = "3.14159";
  30. const char* pi_end = pi + strlen(pi);
  31. for (auto s : state) {
  32. benchmark::DoNotOptimize(pi);
  33. double v;
  34. absl::from_chars(pi, pi_end, v);
  35. benchmark::DoNotOptimize(v);
  36. }
  37. }
  38. BENCHMARK(BM_Absl_Pi);
  39. void BM_Strtod_Pi_float(benchmark::State& state) {
  40. const char* pi = "3.14159";
  41. for (auto s : state) {
  42. benchmark::DoNotOptimize(pi);
  43. benchmark::DoNotOptimize(strtof(pi, nullptr));
  44. }
  45. }
  46. BENCHMARK(BM_Strtod_Pi_float);
  47. void BM_Absl_Pi_float(benchmark::State& state) {
  48. const char* pi = "3.14159";
  49. const char* pi_end = pi + strlen(pi);
  50. for (auto s : state) {
  51. benchmark::DoNotOptimize(pi);
  52. float v;
  53. absl::from_chars(pi, pi_end, v);
  54. benchmark::DoNotOptimize(v);
  55. }
  56. }
  57. BENCHMARK(BM_Absl_Pi_float);
  58. void BM_Strtod_HardLarge(benchmark::State& state) {
  59. const char* num = "272104041512242479.e200";
  60. for (auto s : state) {
  61. benchmark::DoNotOptimize(num);
  62. benchmark::DoNotOptimize(strtod(num, nullptr));
  63. }
  64. }
  65. BENCHMARK(BM_Strtod_HardLarge);
  66. void BM_Absl_HardLarge(benchmark::State& state) {
  67. const char* numstr = "272104041512242479.e200";
  68. const char* numstr_end = numstr + strlen(numstr);
  69. for (auto s : state) {
  70. benchmark::DoNotOptimize(numstr);
  71. double v;
  72. absl::from_chars(numstr, numstr_end, v);
  73. benchmark::DoNotOptimize(v);
  74. }
  75. }
  76. BENCHMARK(BM_Absl_HardLarge);
  77. void BM_Strtod_HardSmall(benchmark::State& state) {
  78. const char* num = "94080055902682397.e-242";
  79. for (auto s : state) {
  80. benchmark::DoNotOptimize(num);
  81. benchmark::DoNotOptimize(strtod(num, nullptr));
  82. }
  83. }
  84. BENCHMARK(BM_Strtod_HardSmall);
  85. void BM_Absl_HardSmall(benchmark::State& state) {
  86. const char* numstr = "94080055902682397.e-242";
  87. const char* numstr_end = numstr + strlen(numstr);
  88. for (auto s : state) {
  89. benchmark::DoNotOptimize(numstr);
  90. double v;
  91. absl::from_chars(numstr, numstr_end, v);
  92. benchmark::DoNotOptimize(v);
  93. }
  94. }
  95. BENCHMARK(BM_Absl_HardSmall);
  96. void BM_Strtod_HugeMantissa(benchmark::State& state) {
  97. std::string huge(200, '3');
  98. const char* num = huge.c_str();
  99. for (auto s : state) {
  100. benchmark::DoNotOptimize(num);
  101. benchmark::DoNotOptimize(strtod(num, nullptr));
  102. }
  103. }
  104. BENCHMARK(BM_Strtod_HugeMantissa);
  105. void BM_Absl_HugeMantissa(benchmark::State& state) {
  106. std::string huge(200, '3');
  107. const char* num = huge.c_str();
  108. const char* num_end = num + 200;
  109. for (auto s : state) {
  110. benchmark::DoNotOptimize(num);
  111. double v;
  112. absl::from_chars(num, num_end, v);
  113. benchmark::DoNotOptimize(v);
  114. }
  115. }
  116. BENCHMARK(BM_Absl_HugeMantissa);
  117. std::string MakeHardCase(int length) {
  118. // The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and
  119. // the next larger representable number. The digits of this number are in
  120. // the string below.
  121. const std::string digits =
  122. "1."
  123. "152113937042223790993097181572444900347587985074226836242307364987727724"
  124. "831384300183638649152607195040591791364113930628852279348613864894524591"
  125. "272746490313676832900762939595690019745859128071117417798540258114233761"
  126. "012939937017879509401007964861774960297319002612457273148497158989073482"
  127. "171377406078223015359818300988676687994537274548940612510414856761641652"
  128. "513434981938564294004070500716200446656421722229202383105446378511678258"
  129. "370570631774499359748259931676320916632111681001853983492795053244971606"
  130. "922718923011680846577744433974087653954904214152517799883551075537146316"
  131. "168973685866425605046988661997658648354773076621610279716804960009043764"
  132. "038392994055171112475093876476783502487512538082706095923790634572014823"
  133. "78877699375152587890625" +
  134. std::string(5000, '0');
  135. // generate the hard cases on either side for the given length.
  136. // Lengths between 3 and 1000 are reasonable.
  137. return digits.substr(0, length) + "1e-297";
  138. }
  139. void BM_Strtod_Big_And_Difficult(benchmark::State& state) {
  140. std::string testcase = MakeHardCase(state.range(0));
  141. const char* begin = testcase.c_str();
  142. for (auto s : state) {
  143. benchmark::DoNotOptimize(begin);
  144. benchmark::DoNotOptimize(strtod(begin, nullptr));
  145. }
  146. }
  147. BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
  148. void BM_Absl_Big_And_Difficult(benchmark::State& state) {
  149. std::string testcase = MakeHardCase(state.range(0));
  150. const char* begin = testcase.c_str();
  151. const char* end = begin + testcase.size();
  152. for (auto s : state) {
  153. benchmark::DoNotOptimize(begin);
  154. double v;
  155. absl::from_chars(begin, end, v);
  156. benchmark::DoNotOptimize(v);
  157. }
  158. }
  159. BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
  160. } // namespace
  161. // ------------------------------------------------------------------------
  162. // Benchmark Time CPU Iterations
  163. // ------------------------------------------------------------------------
  164. // BM_Strtod_Pi 96 ns 96 ns 6337454
  165. // BM_Absl_Pi 35 ns 35 ns 20031996
  166. // BM_Strtod_Pi_float 91 ns 91 ns 7745851
  167. // BM_Absl_Pi_float 35 ns 35 ns 20430298
  168. // BM_Strtod_HardLarge 133 ns 133 ns 5288341
  169. // BM_Absl_HardLarge 181 ns 181 ns 3855615
  170. // BM_Strtod_HardSmall 279 ns 279 ns 2517243
  171. // BM_Absl_HardSmall 287 ns 287 ns 2458744
  172. // BM_Strtod_HugeMantissa 433 ns 433 ns 1604293
  173. // BM_Absl_HugeMantissa 160 ns 160 ns 4403671
  174. // BM_Strtod_Big_And_Difficult/3 236 ns 236 ns 2942496
  175. // BM_Strtod_Big_And_Difficult/8 232 ns 232 ns 2983796
  176. // BM_Strtod_Big_And_Difficult/64 437 ns 437 ns 1591951
  177. // BM_Strtod_Big_And_Difficult/512 1738 ns 1738 ns 402519
  178. // BM_Strtod_Big_And_Difficult/4096 3943 ns 3943 ns 176128
  179. // BM_Strtod_Big_And_Difficult/5000 4397 ns 4397 ns 157878
  180. // BM_Absl_Big_And_Difficult/3 39 ns 39 ns 17799583
  181. // BM_Absl_Big_And_Difficult/8 43 ns 43 ns 16096859
  182. // BM_Absl_Big_And_Difficult/64 550 ns 550 ns 1259717
  183. // BM_Absl_Big_And_Difficult/512 4167 ns 4167 ns 171414
  184. // BM_Absl_Big_And_Difficult/4096 9160 ns 9159 ns 76297
  185. // BM_Absl_Big_And_Difficult/5000 9738 ns 9738 ns 70140