aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/strings/charconv_benchmark.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-06-18 13:18:53 -0700
committerGravatar Shaindel Schwartz <shaindel@google.com>2018-06-18 16:20:03 -0400
commitbd40a41cc142b36c73b881099d08a9d83f7f4780 (patch)
treea73a9bbdfdb823edee52f3b8a2973c4e240ceecc /absl/strings/charconv_benchmark.cc
parentf44e1eed08cd7871de4fb7c40cae27c6f727455d (diff)
--
f28d30df5769bb832dec3ff36d2fcd2bcdf494a3 by Shaindel Schwartz <shaindel@google.com>: Internal change PiperOrigin-RevId: 201046831 -- 711715a78b7e53dfaafd4d7f08a74e76db22af88 by Mark Barolak <mbar@google.com>: Internal fix PiperOrigin-RevId: 201043684 -- 64b53edd6bf1fa48f74e7f5d33f00f80d5089147 by Shaindel Schwartz <shaindel@google.com>: Remove extra whitespace PiperOrigin-RevId: 201041989 -- 0bdd2a0b33657b688e4a04aeba9ebba47e4dc6ca by Shaindel Schwartz <shaindel@google.com>: Whitespace fix. PiperOrigin-RevId: 201034413 -- 3deb0ac296ef1b74c4789e114a8a8bf53253f26b by Shaindel Schwartz <shaindel@google.com>: Scrub build tags. No functional changes. PiperOrigin-RevId: 201032927 -- da75d0f8b73baa7e8f4e9a092bba546012ed3b71 by Alex Strelnikov <strel@google.com>: Internal change. PiperOrigin-RevId: 201026131 -- 6815d80caa19870d0c441b6b9816c68db41393a5 by Tom Manshreck <shreck@google.com>: Add documentation for our LTS snapshot branches PiperOrigin-RevId: 201025191 -- 64c3b02006f39e6a8127bbabf9ec947fb45b6504 by Greg Falcon <gfalcon@google.com>: Provide absl::from_chars for double and float types. This is a forward-compatible implementation of std::from_chars from C++17. This provides exact "round_to_nearest" conversions, and has some nice properties: * Works with string_view (it can convert numbers from non-NUL-terminated buffers) * Never allocates memory * Faster than the standard library strtod() in our toolchain * Uses integer math in its calculations, so is unaffected by floating point environment * Unaffected by C locale Also change SimpleAtod/SimpleAtoi to use this new API under the hood. PiperOrigin-RevId: 201003324 -- 542869258eb100779497c899103dc96aced52749 by Greg Falcon <gfalcon@google.com>: Internal change PiperOrigin-RevId: 200999200 -- 3aba192775c7f80e2cd7f221b0a73537823c54ea by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 200947470 -- daf9b9feedd748d5364a4c06165b7cb7604d3e1e by Mark Barolak <mbar@google.com>: Add an absl:: qualification to a usage of base_internal::SchedulingMode outside of an absl:: namespace. PiperOrigin-RevId: 200748234 -- a8d265290a22d629f3d9bf9f872c204200bfe8c8 by Mark Barolak <mbar@google.com>: Add a missing namespace closing comment to optional.h. PiperOrigin-RevId: 200739934 -- f05af8ee1c6b864dad2df7c907d424209a3e3202 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 200719115 GitOrigin-RevId: f28d30df5769bb832dec3ff36d2fcd2bcdf494a3 Change-Id: Ie4fa601078fd4aa57286372611f1d114fdec82c0
Diffstat (limited to 'absl/strings/charconv_benchmark.cc')
-rw-r--r--absl/strings/charconv_benchmark.cc204
1 files changed, 204 insertions, 0 deletions
diff --git a/absl/strings/charconv_benchmark.cc b/absl/strings/charconv_benchmark.cc
new file mode 100644
index 0000000..fd83f44
--- /dev/null
+++ b/absl/strings/charconv_benchmark.cc
@@ -0,0 +1,204 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/strings/charconv.h"
+
+#include <cstdlib>
+#include <cstring>
+#include <string>
+
+#include "benchmark/benchmark.h"
+
+namespace {
+
+void BM_Strtod_Pi(benchmark::State& state) {
+ const char* pi = "3.14159";
+ for (auto s : state) {
+ benchmark::DoNotOptimize(pi);
+ benchmark::DoNotOptimize(strtod(pi, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_Pi);
+
+void BM_Absl_Pi(benchmark::State& state) {
+ const char* pi = "3.14159";
+ const char* pi_end = pi + strlen(pi);
+ for (auto s : state) {
+ benchmark::DoNotOptimize(pi);
+ double v;
+ absl::from_chars(pi, pi_end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_Pi);
+
+void BM_Strtod_Pi_float(benchmark::State& state) {
+ const char* pi = "3.14159";
+ for (auto s : state) {
+ benchmark::DoNotOptimize(pi);
+ benchmark::DoNotOptimize(strtof(pi, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_Pi_float);
+
+void BM_Absl_Pi_float(benchmark::State& state) {
+ const char* pi = "3.14159";
+ const char* pi_end = pi + strlen(pi);
+ for (auto s : state) {
+ benchmark::DoNotOptimize(pi);
+ float v;
+ absl::from_chars(pi, pi_end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_Pi_float);
+
+void BM_Strtod_HardLarge(benchmark::State& state) {
+ const char* num = "272104041512242479.e200";
+ for (auto s : state) {
+ benchmark::DoNotOptimize(num);
+ benchmark::DoNotOptimize(strtod(num, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_HardLarge);
+
+void BM_Absl_HardLarge(benchmark::State& state) {
+ const char* numstr = "272104041512242479.e200";
+ const char* numstr_end = numstr + strlen(numstr);
+ for (auto s : state) {
+ benchmark::DoNotOptimize(numstr);
+ double v;
+ absl::from_chars(numstr, numstr_end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_HardLarge);
+
+void BM_Strtod_HardSmall(benchmark::State& state) {
+ const char* num = "94080055902682397.e-242";
+ for (auto s : state) {
+ benchmark::DoNotOptimize(num);
+ benchmark::DoNotOptimize(strtod(num, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_HardSmall);
+
+void BM_Absl_HardSmall(benchmark::State& state) {
+ const char* numstr = "94080055902682397.e-242";
+ const char* numstr_end = numstr + strlen(numstr);
+ for (auto s : state) {
+ benchmark::DoNotOptimize(numstr);
+ double v;
+ absl::from_chars(numstr, numstr_end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_HardSmall);
+
+void BM_Strtod_HugeMantissa(benchmark::State& state) {
+ std::string huge(200, '3');
+ const char* num = huge.c_str();
+ for (auto s : state) {
+ benchmark::DoNotOptimize(num);
+ benchmark::DoNotOptimize(strtod(num, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_HugeMantissa);
+
+void BM_Absl_HugeMantissa(benchmark::State& state) {
+ std::string huge(200, '3');
+ const char* num = huge.c_str();
+ const char* num_end = num + 200;
+ for (auto s : state) {
+ benchmark::DoNotOptimize(num);
+ double v;
+ absl::from_chars(num, num_end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_HugeMantissa);
+
+std::string MakeHardCase(int length) {
+ // The number 1.1521...e-297 is exactly halfway between 12345 * 2**-1000 and
+ // the next larger representable number. The digits of this number are in
+ // the std::string below.
+ const std::string digits =
+ "1."
+ "152113937042223790993097181572444900347587985074226836242307364987727724"
+ "831384300183638649152607195040591791364113930628852279348613864894524591"
+ "272746490313676832900762939595690019745859128071117417798540258114233761"
+ "012939937017879509401007964861774960297319002612457273148497158989073482"
+ "171377406078223015359818300988676687994537274548940612510414856761641652"
+ "513434981938564294004070500716200446656421722229202383105446378511678258"
+ "370570631774499359748259931676320916632111681001853983492795053244971606"
+ "922718923011680846577744433974087653954904214152517799883551075537146316"
+ "168973685866425605046988661997658648354773076621610279716804960009043764"
+ "038392994055171112475093876476783502487512538082706095923790634572014823"
+ "78877699375152587890625" +
+ std::string(5000, '0');
+ // generate the hard cases on either side for the given length.
+ // Lengths between 3 and 1000 are reasonable.
+ return digits.substr(0, length) + "1e-297";
+}
+
+void BM_Strtod_Big_And_Difficult(benchmark::State& state) {
+ std::string testcase = MakeHardCase(state.range(0));
+ const char* begin = testcase.c_str();
+ for (auto s : state) {
+ benchmark::DoNotOptimize(begin);
+ benchmark::DoNotOptimize(strtod(begin, nullptr));
+ }
+}
+BENCHMARK(BM_Strtod_Big_And_Difficult)->Range(3, 5000);
+
+void BM_Absl_Big_And_Difficult(benchmark::State& state) {
+ std::string testcase = MakeHardCase(state.range(0));
+ const char* begin = testcase.c_str();
+ const char* end = begin + testcase.size();
+ for (auto s : state) {
+ benchmark::DoNotOptimize(begin);
+ double v;
+ absl::from_chars(begin, end, v);
+ benchmark::DoNotOptimize(v);
+ }
+}
+BENCHMARK(BM_Absl_Big_And_Difficult)->Range(3, 5000);
+
+} // namespace
+
+// ------------------------------------------------------------------------
+// Benchmark Time CPU Iterations
+// ------------------------------------------------------------------------
+// BM_Strtod_Pi 96 ns 96 ns 6337454
+// BM_Absl_Pi 35 ns 35 ns 20031996
+// BM_Strtod_Pi_float 91 ns 91 ns 7745851
+// BM_Absl_Pi_float 35 ns 35 ns 20430298
+// BM_Strtod_HardLarge 133 ns 133 ns 5288341
+// BM_Absl_HardLarge 181 ns 181 ns 3855615
+// BM_Strtod_HardSmall 279 ns 279 ns 2517243
+// BM_Absl_HardSmall 287 ns 287 ns 2458744
+// BM_Strtod_HugeMantissa 433 ns 433 ns 1604293
+// BM_Absl_HugeMantissa 160 ns 160 ns 4403671
+// BM_Strtod_Big_And_Difficult/3 236 ns 236 ns 2942496
+// BM_Strtod_Big_And_Difficult/8 232 ns 232 ns 2983796
+// BM_Strtod_Big_And_Difficult/64 437 ns 437 ns 1591951
+// BM_Strtod_Big_And_Difficult/512 1738 ns 1738 ns 402519
+// BM_Strtod_Big_And_Difficult/4096 3943 ns 3943 ns 176128
+// BM_Strtod_Big_And_Difficult/5000 4397 ns 4397 ns 157878
+// BM_Absl_Big_And_Difficult/3 39 ns 39 ns 17799583
+// BM_Absl_Big_And_Difficult/8 43 ns 43 ns 16096859
+// BM_Absl_Big_And_Difficult/64 550 ns 550 ns 1259717
+// BM_Absl_Big_And_Difficult/512 4167 ns 4167 ns 171414
+// BM_Absl_Big_And_Difficult/4096 9160 ns 9159 ns 76297
+// BM_Absl_Big_And_Difficult/5000 9738 ns 9738 ns 70140