From 4b750cf3c4ec2ce6f13d3c7381194f481a31d06d Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Mon, 23 Nov 2015 11:50:20 -0800 Subject: [PATCH] Benchmark std::to_string vs. folly::to Summary: We wanted to compare the benefits of using folly::to directly, and decided this benchmarking might as well be contributed back to folly. Here are the results: ``` ============================================================================ folly/test/ConvTest.cpp relative time/iter iters/s ============================================================================ ... ---------------------------------------------------------------------------- u64ToStringClibMeasure(1) 157.88ns 6.33M u64ToStringFollyMeasure(1) 212.52% 74.29ns 13.46M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12) 158.14ns 6.32M u64ToStringFollyMeasure(12) 212.55% 74.40ns 13.44M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123) 152.26ns 6.57M u64ToStringFollyMeasure(123) 210.95% 72.18ns 13.85M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234) 152.40ns 6.56M u64ToStringFollyMeasure(1234) 204.82% 74.41ns 13.44M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345) 156.01ns 6.41M u64ToStringFollyMeasure(12345) 208.90% 74.68ns 13.39M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123456) 178.21ns 5.61M u64ToStringFollyMeasure(123456) 238.77% 74.63ns 13.40M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234567) 161.38ns 6.20M u64ToStringFollyMeasure(1234567) 203.09% 79.46ns 12.58M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345678) 159.01ns 6.29M u64ToStringFollyMeasure(12345678) 192.42% 82.64ns 12.10M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123456789) 169.07ns 5.91M u64ToStringFollyMeasure(123456789) 203.70% 83.00ns 12.05M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234567890) 166.56ns 6.00M u64ToStringFollyMeasure(1234567890) 187.21% 88.97ns 11.24M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345678901) 176.22ns 5.67M u64ToStringFollyMeasure(12345678901) 197.57% 89.20ns 11.21M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123456789012) 174.74ns 5.72M u64ToStringFollyMeasure(123456789012) 182.52% 95.74ns 10.45M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234567890123) 178.47ns 5.60M u64ToStringFollyMeasure(1234567890123) 186.81% 95.54ns 10.47M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345678901234) 187.33ns 5.34M u64ToStringFollyMeasure(12345678901234) 184.05% 101.78ns 9.82M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123456789012345) 198.16ns 5.05M u64ToStringFollyMeasure(123456789012345) 182.48% 108.59ns 9.21M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234567890123456) 182.56ns 5.48M u64ToStringFollyMeasure(1234567890123456) 164.19% 111.19ns 8.99M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345678901234567) 186.80ns 5.35M u64ToStringFollyMeasure(12345678901234567) 162.33% 115.07ns 8.69M ---------------------------------------------------------------------------- u64ToStringClibMeasure(123456789012345678) 195.08ns 5.13M u64ToStringFollyMeasure(123456789012345678) 165.51% 117.87ns 8.48M ---------------------------------------------------------------------------- u64ToStringClibMeasure(1234567890123456789) 200.27ns 4.99M u64ToStringFollyMeasure(1234567890123456789) 164.62% 121.65ns 8.22M ---------------------------------------------------------------------------- u64ToStringClibMeasure(12345678901234567890U) 203.18ns 4.92M u64ToStringFollyMeasure(12345678901234567890U) 162.38% 125.13ns 7.99M ---------------------------------------------------------------------------- ... ============================================================================ ``` Reviewed By: yfeldblum Differential Revision: D2680998 fb-gh-sync-id: 179fbd756c714c92b7230d3750af024865ecbb0a --- folly/test/ConvTest.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/folly/test/ConvTest.cpp b/folly/test/ConvTest.cpp index 8967a7d0..3cf6d5b8 100644 --- a/folly/test/ConvTest.cpp +++ b/folly/test/ConvTest.cpp @@ -1021,6 +1021,22 @@ void u64ToAsciiFollyBM(unsigned int n, uint64_t value) { } } +// Benchmark unsigned to string conversion + +void u64ToStringClibMeasure(unsigned int n, uint64_t value) { + // FOLLY_RANGE_CHECK_TO_STRING expands to std::to_string, except on Android + // where std::to_string is not supported + FOR_EACH_RANGE (i, 0, n) { + FOLLY_RANGE_CHECK_TO_STRING(value + n); + } +} + +void u64ToStringFollyMeasure(unsigned int n, uint64_t value) { + FOR_EACH_RANGE (i, 0, n) { + to(value + n); + } +} + // Benchmark uitoa with string append void u2aAppendClassicBM(unsigned int n, uint64_t value) { @@ -1130,6 +1146,34 @@ DEFINE_BENCHMARK_GROUP(12345678901234567890U); #undef DEFINE_BENCHMARK_GROUP +#define DEFINE_BENCHMARK_GROUP(n) \ + BENCHMARK_PARAM(u64ToStringClibMeasure, n); \ + BENCHMARK_RELATIVE_PARAM(u64ToStringFollyMeasure, n); \ + BENCHMARK_DRAW_LINE(); + +DEFINE_BENCHMARK_GROUP(1); +DEFINE_BENCHMARK_GROUP(12); +DEFINE_BENCHMARK_GROUP(123); +DEFINE_BENCHMARK_GROUP(1234); +DEFINE_BENCHMARK_GROUP(12345); +DEFINE_BENCHMARK_GROUP(123456); +DEFINE_BENCHMARK_GROUP(1234567); +DEFINE_BENCHMARK_GROUP(12345678); +DEFINE_BENCHMARK_GROUP(123456789); +DEFINE_BENCHMARK_GROUP(1234567890); +DEFINE_BENCHMARK_GROUP(12345678901); +DEFINE_BENCHMARK_GROUP(123456789012); +DEFINE_BENCHMARK_GROUP(1234567890123); +DEFINE_BENCHMARK_GROUP(12345678901234); +DEFINE_BENCHMARK_GROUP(123456789012345); +DEFINE_BENCHMARK_GROUP(1234567890123456); +DEFINE_BENCHMARK_GROUP(12345678901234567); +DEFINE_BENCHMARK_GROUP(123456789012345678); +DEFINE_BENCHMARK_GROUP(1234567890123456789); +DEFINE_BENCHMARK_GROUP(12345678901234567890U); + +#undef DEFINE_BENCHMARK_GROUP + #define DEFINE_BENCHMARK_GROUP(n) \ BENCHMARK_PARAM(clibAtoiMeasure, n); \ BENCHMARK_RELATIVE_PARAM(lexicalCastMeasure, n); \ -- 2.34.1