Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt::StringLiterals
Forum Updated to NodeBB v4.3 + New Features

Qt::StringLiterals

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 276 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Redman
    wrote last edited by
    #1
    using namespace Qt::StringLiterals;
    
    //1
    qInfo() << u"Hello"_s;
    
    //2
    QString name = "Tim";
    qInfo() << u"Hello %1"_s.arg(name);
    

    1:
    Equal to QStringLiteral

    2:
    What happens here?
    Is this some kind of hybrid where Hello %1 is compiled but during runtime a new QString is created anyway because of the argument?
    Is this even benefitial?

    1 Reply Last reply
    0
    • kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote last edited by
      #4

      But there is a 50% performance increase anyway right?

      The exact overhead is not that easy to guess, as some other things come into play. First, even for u""_s, there is some constant overhead for constructing a QString object. And second, there is also other overhead, like looking up %1 ...

      But u""_g should always be faster than using just QString(const char*), as the latter needs to a) calculate the length of const char * by looking for the final NULL character, and b) convert UTF-8 to UTF-16. Mind you, these are very optimized operations, so the savings in a typical app aren't typically dramatic. But if you can avoid even some overhead by just using convenient API, why wouldn't you?

      Director R&D, The Qt Company

      1 Reply Last reply
      5
      • kkoehneK Offline
        kkoehneK Offline
        kkoehne
        Moderators
        wrote last edited by kkoehne
        #2

        Is this some kind of hybrid where Hello %1 is compiled but during runtime a new QString is created anyway
        because of the argument?

        Exactly.

        u"Hello %1"_s will create a QString with "Hello %1" as content at compile time. At runtime, QString::arg() is then called, which will generate a new QString "Hello Tim".

        Is this even benefitial?

        In this snippet, no. You might as well just write u"Hello Tim"_s. You typically use arg() if the arguments are not determined at compile time.

        Director R&D, The Qt Company

        R 1 Reply Last reply
        3
        • kkoehneK kkoehne

          Is this some kind of hybrid where Hello %1 is compiled but during runtime a new QString is created anyway
          because of the argument?

          Exactly.

          u"Hello %1"_s will create a QString with "Hello %1" as content at compile time. At runtime, QString::arg() is then called, which will generate a new QString "Hello Tim".

          Is this even benefitial?

          In this snippet, no. You might as well just write u"Hello Tim"_s. You typically use arg() if the arguments are not determined at compile time.

          R Offline
          R Offline
          Redman
          wrote last edited by Redman
          #3

          @kkoehne
          But there is a 50% performance increase anyway right?

          Taking the precompiled string and creating a new QString as a result of .arg() is more efficient that
          creating QString("Hello %1") and then creating another one with arg(). In the docs it says that arg returns a new QString, which makes the QString("Hello %1") approach 2x as expensive as the first.

          The Tim example is a simplification of a string that is not know during compile time.

          1 Reply Last reply
          0
          • kkoehneK Offline
            kkoehneK Offline
            kkoehne
            Moderators
            wrote last edited by
            #4

            But there is a 50% performance increase anyway right?

            The exact overhead is not that easy to guess, as some other things come into play. First, even for u""_s, there is some constant overhead for constructing a QString object. And second, there is also other overhead, like looking up %1 ...

            But u""_g should always be faster than using just QString(const char*), as the latter needs to a) calculate the length of const char * by looking for the final NULL character, and b) convert UTF-8 to UTF-16. Mind you, these are very optimized operations, so the savings in a typical app aren't typically dramatic. But if you can avoid even some overhead by just using convenient API, why wouldn't you?

            Director R&D, The Qt Company

            1 Reply Last reply
            5
            • R Redman has marked this topic as solved

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved