Home > database >  Improve the performance of a method used to convert an std::string into an std::wstring?
Improve the performance of a method used to convert an std::string into an std::wstring?

Time:09-07

I have the following method used to convert an std::string object into an std::wstring one:

#include <string>
#include <type_traits>
#include <locale>
#include <codecvt>

template <class CharT>
inline std::basic_string<CharT> StringConverter( std::string input_str )
 {
  if constexpr( std::is_same_v <CharT, char> ) return input_str;
  else if constexpr( std::is_same_v <CharT, wchar_t> )
   {
    static std::wstring_convert <std::codecvt_utf8_utf16 <wchar_t>> converter;
    return converter.from_bytes( input_str );
   }
  return StringConverter<CharT>( "" );
 }

This is used only to convert simple strings like:

#include <string>
#include <iostream>
using namespace std::literals::string_literals;

int main()
 {
  std::cout << StringConverter<wchar_t>( "Hello"s ); // returns L"Hello"
  std::cout << StringConverter<char>( "Hello"s ); // returns "Hello"
 }

and works well. However it seems a bit too much expensive in terms of performance: in the first case it needs 64 ns to convert the string, while in the second case 11.9. Do you know if there is a better way to write it or if it can be improved in C 17? Thanks.

CodePudding user response:

I'm not sure it is possible to improve performances of it but you sure can improve a bit on other grounds.

If you rely on template specialization, you can make the code a bit simpler (readable).

template <typename CharT>
std::basic_string<CharT> StringConverter(const std::string & input_str);

template <>
std::string StringConverter(const std::string & input_str)
{
    return input_str;
}

template <>
std::wstring StringConverter(const std::string & input_str)
{
    static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.from_bytes(input_str);
}

Your function now works only for the two given specializations and consequently you don't need that ugly return statement to handle the case it is called with something else than char or wchar_t.

Moreover, taking the argument through a const reference instead of a plain copy may improve performances a bit.

Note: Since , std::codecvt_utf8_utf16 seems to be deprecated.

  • Related