// Copyright 2020 The Division 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 // // https://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. // Author dietoad@gmail.com && firedtoad@gmail.com #include #include #include #include #include #include #include #include #include #include #include uint64_t tag = 1234567890123456; float tagf = M_PI; template void NumberToString(T &t, std::string &str) { S oss; oss.str(""); oss << t; str = oss.str(); } template static void BenchStreamStringFrom(benchmark::State &state) { std::string r; for (auto _ : state) { NumberToString(tag, r); benchmark::DoNotOptimize(r); } } template static void BenchStreamStringFromF(benchmark::State &state) { std::string r; for (auto _ : state) { NumberToString(tagf, r); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchStreamStringFrom, std::stringstream, uint64_t); BENCHMARK_TEMPLATE(BenchStreamStringFrom, std::ostringstream, uint64_t); BENCHMARK_TEMPLATE(BenchStreamStringFromF, std::stringstream, float); BENCHMARK_TEMPLATE(BenchStreamStringFromF, std::ostringstream, float); template static void BenchItoa(benchmark::State &state) { for (auto _ : state) { char buf[64]{}; auto r = snprintf(buf, 64, "%lu", tag); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchItoa, uint64_t); template static void BenchGcvt(benchmark::State &state) { for (auto _ : state) { char buf[64]{}; auto r = gcvt(tagf, 11, buf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchGcvt, float); template static void BenchToString(benchmark::State &state) { for (auto _ : state) { auto r = std::to_string(tag); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchToString, uint64_t); template static void BenchToStringF(benchmark::State &state) { for (auto _ : state) { auto r = std::to_string(tagf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchToStringF, float); template static void BenchStdToChars(benchmark::State &state) { for (auto _ : state) { char buf[64]{}; auto r = std::to_chars(buf, buf + 64, tag, 10); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchStdToChars, uint64_t); template static void BenchStdToCharsF(benchmark::State &state) { for (auto _ : state) { char buf[64]{}; auto r = std::to_chars(buf, buf + 64, (uint64_t)tagf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchStdToCharsF, float); template static void BenchBoostStringFrom(benchmark::State &state) { for (auto _ : state) { T r = boost::lexical_cast(tag); benchmark::DoNotOptimize(r); } } template static void BenchBoostStringTof(benchmark::State &state) { for (auto _ : state) { T r = boost::lexical_cast(tagf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchBoostStringFrom, std::string); BENCHMARK_TEMPLATE(BenchBoostStringTof, std::string); template static void BenchAbFastIntToBuffer(benchmark::State &state) { T r = 0; for (auto _ : state) { char buf[64]{}; if (absl::numbers_internal::FastIntToBuffer(tag, buf)) { } benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchAbFastIntToBuffer, uint64_t); template static void BenchAbSixDigitsToBuffer(benchmark::State &state) { T r = 0; for (auto _ : state) { char buf[64]{}; if (absl::numbers_internal::SixDigitsToBuffer(tagf, buf)) { } benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchAbSixDigitsToBuffer, float); template static void BenchAbStrFormat(benchmark::State &state) { for (auto _ : state) { auto r = absl::StrFormat("%d", tag); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchAbStrFormat, uint64_t); template static void BenchAbStrFormatF(benchmark::State &state) { for (auto _ : state) { auto r = absl::StrFormat("%f", tagf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchAbStrFormatF, float); template static void BenchPBSimpleItoa(benchmark::State &state) { T r = 0; for (auto _ : state) { char buf[64]{}; auto r = google::protobuf::FastUInt64ToBuffer(tag, buf); benchmark::DoNotOptimize(r); } } BENCHMARK_TEMPLATE(BenchPBSimpleItoa, uint64_t); int main(int argc, char **argv) { tag = std::mt19937_64{std::random_device{}()}(); tagf = std::mt19937_64{std::random_device{}()}(); benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); return 0; }