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. QStringLiteral: be or not to be!
Forum Updated to NodeBB v4.3 + New Features

QStringLiteral: be or not to be!

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.5k 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.
  • rkhaotixR Offline
    rkhaotixR Offline
    rkhaotix
    wrote on last edited by
    #1

    Hello fellows,

    I was studying about QStringLiteral and started to replace the literals that I have on my project's code by that macro. Until now, I don't see any performance gain... But I was wondering: What are the real benefits in use it? In which cases QStringLiteral performs better than QString or QLatin1String?

    I found the docs for QStringLiteral a bit vague, read a few posts on Google but I'm still full of doubts, so I'm here to ask for help. Well, let's put some examples:

    1. String initialization
      @
      QString foo = "bar";
      QString foo = QString("bar");
      QString foo = QStringLiteral("bar");
      @

    2. String concatenation
      @
      QString dummy = "somestring";
      QString foo;

    foo = "bar" + dummy;
    foo = QString("bar") + dummy;
    foo = QStringLiteral("bar") + dummy;
    @

    1. Function call with QString parameters

    @
    QString foo = "bar";

    //Let's assume that startsWith() accepts only QString as parameter.
    foo.startsWith("bar");
    foo.startsWith(QString("bar"));
    foo.startsWith(QStringLiteral("bar"));
    foo.startsWith(QLatin1String("bar"));
    @

    Based upon the examples above in which cases I can use QStringLiteral in order to see the real advantages of it?

    Thank you!
    P.S.: Sorry about my poor English! :)
    P.S.1: My project's site: http://pgmodeler.com.br

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      Hi,

      You may find the discussion "here":http://comments.gmane.org/gmane.comp.lib.qt.qt5-feedback/1368 useful. You have propably found it but there is also "this":http://blog.qt.digia.com/blog/2014/06/13/qt-weekly-13-qstringliteral/ blog post.

      I haven't done any benchmarks myself so I don't know how you gain or lose in each scenario. But note that QStringLiteral is about creating QString instances, which are UTF-16 encoded, from string literals encoded differently in the source file. So it essentially pushes string creation to compile time.

      Also note that QtCore developers have already spent a great deal of time to optimize string processing/conversions/etc. So QString is already very fast. I don't think you would notice significant performance changes in your applications unless you have tight and long-running string handling loops where using QStringLiteral could avoid string creation at each iteration. (Reading a large file line by line and processing wouldn't count as an example because file IO is much slower than string processing.)

      1 Reply Last reply
      0
      • rkhaotixR Offline
        rkhaotixR Offline
        rkhaotix
        wrote on last edited by
        #3

        Thank you so much for the clarification.
        I have read both links you mentioned before create this thread.

        bq. But note that QStringLiteral is about creating QString instances, which are UTF-16 encoded, from string literals encoded differently in the source file. So it essentially pushes string creation to compile time.

        OK. Got it. But what I can't understand is why even the docs enforces the usage of the macro instead of QLatin1String or QString ctor like in item 2 line 2. In my point of view the QString(const char*) fits well in the examples above. I may be completely wrong but the little overhead (in my case) does not worth the widespread usage of the verbose macro.

        bq.
        Also note that QtCore developers have already spent a great deal of time to optimize string processing/conversions/etc. So QString is already very fast.

        I believe that too. This is why I'm searching for a consistent answer to explain the advantages of the macro.

        bq. I don’t think you would notice significant performance changes in your applications unless you have tight and long-running string handling loops where using QStringLiteral could avoid string creation at each iteration. (Reading a large file line by line and processing wouldn’t count as an example because file IO is much slower than string processing.)

        That's not the case. My software handles a lot of strings for SQL code generation but the majority of them are const/static ones created at startup time. This can explain why I couldn't see any performance changes.

        Again, thank you for the explanation. I think I'll replace literals by QString ("") just for the sake of readability. :)

        R 1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on last edited by
          #4

          On a related note, you may want to check out "QStringBuilder":http://doc.qt.io/qt-5/qstring.html#more-efficient-string-construction (mentioned towards the end of the linked section) if you concatenate more than 2 strings.

          1 Reply Last reply
          0
          • rkhaotixR rkhaotix

            Thank you so much for the clarification.
            I have read both links you mentioned before create this thread.

            bq. But note that QStringLiteral is about creating QString instances, which are UTF-16 encoded, from string literals encoded differently in the source file. So it essentially pushes string creation to compile time.

            OK. Got it. But what I can't understand is why even the docs enforces the usage of the macro instead of QLatin1String or QString ctor like in item 2 line 2. In my point of view the QString(const char*) fits well in the examples above. I may be completely wrong but the little overhead (in my case) does not worth the widespread usage of the verbose macro.

            bq.
            Also note that QtCore developers have already spent a great deal of time to optimize string processing/conversions/etc. So QString is already very fast.

            I believe that too. This is why I'm searching for a consistent answer to explain the advantages of the macro.

            bq. I don’t think you would notice significant performance changes in your applications unless you have tight and long-running string handling loops where using QStringLiteral could avoid string creation at each iteration. (Reading a large file line by line and processing wouldn’t count as an example because file IO is much slower than string processing.)

            That's not the case. My software handles a lot of strings for SQL code generation but the majority of them are const/static ones created at startup time. This can explain why I couldn't see any performance changes.

            Again, thank you for the explanation. I think I'll replace literals by QString ("") just for the sake of readability. :)

            R Offline
            R Offline
            Rosa
            wrote on last edited by Rosa
            #5

            @rkhaotix As @ckakman said, QStringLiteral is encoded with utf-16, so QStringLiteral is very useful while dealing with non-english charset, such as Arabic charset or EastAsian chartset etc.
            In my case, I wanted to generate a QRcode Image with QByteArray, so I needed to transform my QString to QByteArray.
            some code like follows:

            QString eastAsiaString1 = "中文";
            QString eastAsiaString2 = QStringLiteral("中文");
            qDebug() << "eastAsiaString1:" << eastAsiaString1.toUtf8();
            qDebug() << "eastAsiaString2:" << eastAsiaString2.toUtf8();
            QImage image;
            QRcode* qr = QRcode_encodeString(eastAsiaString2.toUtf8(), 1, QR_ECLEVEL_Q, QR_MODE_8, 0);
            

            The results I got from the console:

            eastAsiaString1: "\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD\xEF\xBF\xBD"
            eastAsiaString2: "\xE4\xB8\xAD\xE6\x96\x87"
            

            eastAsianString1 was different from eastAsianString2, And the result transformed from eastAsianString2 was what I needed.

            1 Reply Last reply
            0

            • Login

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