#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 "Nuclex/Support/Text/LexicalCast.h" #include #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 an std::string in a naive, slow way /// Integer type that will be converted /// Integer that will be formatted into a string /// A string containing a textual representation of the integer template std::string formatNumberNaive(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 return std::string(end, temp + sizeof(temp) - end); } // ------------------------------------------------------------------------------------------- // /// Fast random number generator used in the benchmark std::mt19937_64 randomNumberGenerator; /// Uniform distribution to make the output cover all possible integers std::uniform_int_distribution randomNumberDistribution; // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Support { namespace Text { // ------------------------------------------------------------------------------------------- // BASELINE(Integer32ToString, CxxToString, 1000, 0) { celero::DoNotOptimizeAway( std::to_string( static_cast(randomNumberDistribution(randomNumberGenerator)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Integer32ToString, NaiveDivideBy10, 1000, 0) { celero::DoNotOptimizeAway( formatNumberNaive( static_cast(randomNumberDistribution(randomNumberGenerator)) ) ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(Integer32ToString, NuclexLexicalCast, 1000, 0) { celero::DoNotOptimizeAway( Nuclex::Support::Text::lexical_cast( static_cast(randomNumberDistribution(randomNumberGenerator)) ) ); } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Text