//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17 #include #include #include #include "benchmark/benchmark.h" #include "test_macros.h" // Tests the full range of the value. template static std::array generate(std::uniform_int_distribution distribution = std::uniform_int_distribution{ std::numeric_limits::min(), std::numeric_limits::max()}) { std::mt19937 generator; std::array result; std::generate_n(result.begin(), result.size(), [&] { return distribution(generator); }); return result; } template static void BM_Basic(benchmark::State& state) { std::array data{generate()}; std::array output; while (state.KeepRunningBatch(data.size())) for (auto value : data) benchmark::DoNotOptimize(std::format_to(output.begin(), "{}", value)); } BENCHMARK(BM_Basic); BENCHMARK(BM_Basic); BENCHMARK(BM_Basic); BENCHMARK(BM_Basic); // Ideally the low values of a 128-bit value are all dispatched to a 64-bit routine. #ifndef TEST_HAS_NO_INT128 template static void BM_BasicLow(benchmark::State& state) { using U = std::conditional_t, int64_t, uint64_t>; std::array data{ generate(std::uniform_int_distribution{std::numeric_limits::min(), std::numeric_limits::max()})}; std::array output; while (state.KeepRunningBatch(data.size())) for (auto value : data) benchmark::DoNotOptimize(std::format_to(output.begin(), "{}", value)); } BENCHMARK(BM_BasicLow<__uint128_t>); BENCHMARK(BM_BasicLow<__int128_t>); BENCHMARK(BM_Basic<__uint128_t>); BENCHMARK(BM_Basic<__int128_t>); #endif template inline constexpr std::string to_string = ""; template <> inline constexpr std::string to_string = "int64_t"; template <> inline constexpr std::string to_string = "uint64_t"; int main(int argc, char** argv) { auto bm = [](std::type_identity, std::string fmt) { benchmark::RegisterBenchmark( "std::format(" + to_string + ") (fmt: " + fmt + ")", [fmt](benchmark::State& state) { std::array data = generate(); std::array output; while (state.KeepRunningBatch(data.size())) for (auto value : data) benchmark::DoNotOptimize(std::vformat_to(output.begin(), fmt, std::make_format_args(value))); }); }; bm(std::type_identity(), "{:b}"); bm(std::type_identity(), "{:0<512b}"); bm(std::type_identity(), "{:0^512b}"); bm(std::type_identity(), "{:0>512b}"); bm(std::type_identity(), "{:0512b}"); bm(std::type_identity(), "{:Lb}"); bm(std::type_identity(), "{:0<512Lb}"); bm(std::type_identity(), "{:0^512Lb}"); bm(std::type_identity(), "{:0>512Lb}"); bm(std::type_identity(), "{:0512Lb}"); bm(std::type_identity(), "{:o}"); bm(std::type_identity(), "{:0<512o}"); bm(std::type_identity(), "{:0^512o}"); bm(std::type_identity(), "{:0>512o}"); bm(std::type_identity(), "{:0512o}"); bm(std::type_identity(), "{:Lo}"); bm(std::type_identity(), "{:0<512Lo}"); bm(std::type_identity(), "{:0^512Lo}"); bm(std::type_identity(), "{:0>512Lo}"); bm(std::type_identity(), "{:0512Lo}"); bm(std::type_identity(), "{:d}"); bm(std::type_identity(), "{:0<512d}"); bm(std::type_identity(), "{:0^512d}"); bm(std::type_identity(), "{:0>512d}"); bm(std::type_identity(), "{:0512d}"); bm(std::type_identity(), "{:Ld}"); bm(std::type_identity(), "{:0<512Ld}"); bm(std::type_identity(), "{:0^512Ld}"); bm(std::type_identity(), "{:0>512Ld}"); bm(std::type_identity(), "{:0512Ld}"); bm(std::type_identity(), "{:x}"); bm(std::type_identity(), "{:0<512x}"); bm(std::type_identity(), "{:0^512x}"); bm(std::type_identity(), "{:0>512x}"); bm(std::type_identity(), "{:0512x}"); bm(std::type_identity(), "{:Lx}"); bm(std::type_identity(), "{:0<512Lx}"); bm(std::type_identity(), "{:0^512Lx}"); bm(std::type_identity(), "{:0>512Lx}"); bm(std::type_identity(), "{:0512Lx}"); bm(std::type_identity(), "{:X}"); bm(std::type_identity(), "{:0<512X}"); bm(std::type_identity(), "{:0^512X}"); bm(std::type_identity(), "{:0>512X}"); bm(std::type_identity(), "{:0512X}"); bm(std::type_identity(), "{:LX}"); bm(std::type_identity(), "{:0<512LX}"); bm(std::type_identity(), "{:0^512LX}"); bm(std::type_identity(), "{:0>512LX}"); bm(std::type_identity(), "{:0512LX}"); benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); benchmark::Shutdown(); return 0; }