#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_SUPPORT_SOURCE 1 #include "Nuclex/Support/Config.h" #include #include "./../../Source/Text/NumberFormatter.h" #include // for std::copy_n() #include // for std::mt19937 #include // for std::uint32_t, std::uint64_t #include // for std::string #include // for std::is_signed #include // for std::abs() namespace { // ------------------------------------------------------------------------------------------- // /// Formats a number into a character string in a naive, slow way /// Integer type that will be converted /// Integer that will be formatted into a string /// Integer that will be formatted into a string /// A pointer one past the last character written to the buffer template char *formatNumberNaive(char *buffer, TInteger integer) { char temp[40]; // max 128 bit integer length without terminating \0 // If the integer may be negative, remember it and make it positive if constexpr(std::is_signed::value) { if(integer < 0) { temp[0] = u8'-'; integer = std::abs(integer); } } // Build the integer backwards by successively dividing it by 10 char *end = temp + sizeof(temp) - 1; while(integer >= 10) { *end = static_cast(u8'0' + (integer % 10)); integer /= 10; --end; // go backwards } *end = static_cast(u8'0' + integer); // If the integer may be negative, and was negative, prepend a minus sign if constexpr(std::is_signed::value) { if(temp[0] == u8'-') { --end; *end = u8'-'; } } // Package the generated character in an std::string std::size_t length = temp + sizeof(temp) - end; std::copy_n(end, length, buffer); return buffer + length; } // ------------------------------------------------------------------------------------------- // /// Fast random number generator used in the benchmark std::mt19937 randomNumberGenerator32; /// Uniform distribution to make the output cover all possible integers std::uniform_int_distribution randomNumberDistribution32; /// Fast random number generator used in the benchmark std::mt19937_64 randomNumberGenerator64; /// Uniform distribution to make the output cover all possible integers std::uniform_int_distribution randomNumberDistribution64; /// Distribution for floats in a normalized -1.0 .. +1.0 range std::uniform_real_distribution smallRandomNumberDistributionFloat(-1.0f, +1.0f); /// Distribution for floats using half of their entire numeric range std::uniform_real_distribution largeRandomNumberDistributionFloat( std::numeric_limits::lowest() / 2.1f, std::numeric_limits::max() / 2.1f ); /// Distribution for doubles in a normalized -1.0 .. +1.0 range std::uniform_real_distribution smallRandomNumberDistributionDouble(-1.0f, +1.0f); /// Distribution for doubles using half of their entire numeric range std::uniform_real_distribution largeRandomNumberDistributionDouble( std::numeric_limits::lowest() / 2.1, std::numeric_limits::max() / 2.1 ); // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Support { namespace Text { // ------------------------------------------------------------------------------------------- // BASELINE(Integer32Itoa, NaiveDivideBy10, 1000, 0) { char number[40]; celero::DoNotOptimizeAway( formatNumberNaive( number, static_cast(randomNumberDistribution32(randomNumberGenerator32)) ) ); } // ------------------------------------------------------------------------------------------- // BASELINE(Integer64Itoa, NaiveDivideBy10, 1000, 0) { char number[40]; celero::DoNotOptimizeAway( formatNumberNaive( number, static_cast(randomNumberDistribution64(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // BASELINE(Float32Ftoa_x2, CxxToString, 1000, 0) { celero::DoNotOptimizeAway( std::to_string( static_cast(smallRandomNumberDistributionFloat(randomNumberGenerator64)) ) ); celero::DoNotOptimizeAway( std::to_string( static_cast(largeRandomNumberDistributionFloat(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // BASELINE(Float64Ftoa_x2, CxxToString, 1000, 0) { celero::DoNotOptimizeAway( std::to_string( static_cast(smallRandomNumberDistributionDouble(randomNumberGenerator64)) ) ); celero::DoNotOptimizeAway( std::to_string( static_cast(largeRandomNumberDistributionDouble(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Integer32Itoa, NumberFormatter, 1000, 0) { char number[40]; celero::DoNotOptimizeAway( FormatInteger( number, static_cast(randomNumberDistribution32(randomNumberGenerator32)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Integer64Itoa, NumberFormatter, 1000, 0) { char number[40]; celero::DoNotOptimizeAway( FormatInteger( number, static_cast(randomNumberDistribution64(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Float32Ftoa_x2, NumberFormatter, 1000, 0) { char number[48]; celero::DoNotOptimizeAway( FormatFloat( number, static_cast(smallRandomNumberDistributionFloat(randomNumberGenerator64)) ) ); celero::DoNotOptimizeAway( FormatFloat( number, static_cast(largeRandomNumberDistributionFloat(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Float64Ftoa_x2, NumberFormatter, 1000, 0) { char number[325]; celero::DoNotOptimizeAway( FormatFloat( number, static_cast(smallRandomNumberDistributionDouble(randomNumberGenerator64)) ) ); celero::DoNotOptimizeAway( FormatFloat( number, static_cast(largeRandomNumberDistributionDouble(randomNumberGenerator64)) ) ); } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Text