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 optimization
Forum Update on Tuesday, May 27th 2025

QStringLiteral optimization

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 704 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 on 15 Dec 2023, 08:18 last edited by
    #1

    QStringLiteral creates static data during compilation which safes us the QString overhead. But what about this usecase

    inline const static QString tableName = QStringLiteral("users");
    

    Does using QStringLiteral pose any performance gains here? Would this make the start of my app faster? Static variables get initialized first, so I'd assume we do actually have a performance gain

    C J J 3 Replies Last reply 15 Dec 2023, 08:21
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 15 Dec 2023, 08:52 last edited by
      #9

      I'm gonna go a bit against my colleagues here and say go for it. Avoiding dynamic allocation is always a good thing, especially on platforms on which it's a very costly operation (can go in thousands of cycles on some embedded hardware).
      Even if you don't see much of a difference on your particular platform it's simply a good habit to write code that is performant by default, especially when there's not much difference in code complexity associated with it.

      C 1 Reply Last reply 15 Dec 2023, 09:22
      2
      • R Redman
        15 Dec 2023, 08:18

        QStringLiteral creates static data during compilation which safes us the QString overhead. But what about this usecase

        inline const static QString tableName = QStringLiteral("users");
        

        Does using QStringLiteral pose any performance gains here? Would this make the start of my app faster? Static variables get initialized first, so I'd assume we do actually have a performance gain

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 15 Dec 2023, 08:21 last edited by
        #2

        @Redman said in QStringLiteral optimization:

        Does using QStringLiteral pose any performance gains here?

        Compared to what?

        Would this make the start of my app faster?

        Your app will now start ten cpu cycles faster.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        R 1 Reply Last reply 15 Dec 2023, 08:23
        0
        • C Christian Ehrlicher
          15 Dec 2023, 08:21

          @Redman said in QStringLiteral optimization:

          Does using QStringLiteral pose any performance gains here?

          Compared to what?

          Would this make the start of my app faster?

          Your app will now start ten cpu cycles faster.

          R Offline
          R Offline
          Redman
          wrote on 15 Dec 2023, 08:23 last edited by
          #3

          @Christian-Ehrlicher said in QStringLiteral optimization:

          @Redman said in QStringLiteral optimization:

          Does using QStringLiteral pose any performance gains here?

          Compared to what?

          inline const static QString tableName = "users";

          Would this make the start of my app faster?

          Your app will now start ten cpu cycles faster.

          It's all about the UX

          1 Reply Last reply
          0
          • R Redman
            15 Dec 2023, 08:18

            QStringLiteral creates static data during compilation which safes us the QString overhead. But what about this usecase

            inline const static QString tableName = QStringLiteral("users");
            

            Does using QStringLiteral pose any performance gains here? Would this make the start of my app faster? Static variables get initialized first, so I'd assume we do actually have a performance gain

            J Online
            J Online
            JonB
            wrote on 15 Dec 2023, 08:23 last edited by JonB
            #4

            @Redman
            So far as I know, if you wrote instead inline const static QString tableName = QString("users"); the runtime would still have to call QString() to do the conversion, just it would happen at the start of program execution (depending on where you write this).

            In any case, the difference in start up will be almost irrelevant,. You would need "trillions" of these to make much difference. Worry about this "speed difference" if you are using a line like this in a function/loop where it's going to be called millions of times, not a one-off place.

            If it really bothers you, just use QStringLiteral() on every literal string.

            1 Reply Last reply
            1
            • R Redman
              15 Dec 2023, 08:18

              QStringLiteral creates static data during compilation which safes us the QString overhead. But what about this usecase

              inline const static QString tableName = QStringLiteral("users");
              

              Does using QStringLiteral pose any performance gains here? Would this make the start of my app faster? Static variables get initialized first, so I'd assume we do actually have a performance gain

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Dec 2023, 08:25 last edited by
              #5

              @Redman said in QStringLiteral optimization:

              inline const static QString tableName = QStringLiteral("users");

              Why not

              inline const static QString tableName{"users"};
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 15 Dec 2023, 08:26
              0
              • J jsulm
                15 Dec 2023, 08:25

                @Redman said in QStringLiteral optimization:

                inline const static QString tableName = QStringLiteral("users");

                Why not

                inline const static QString tableName{"users"};
                
                J Online
                J Online
                JonB
                wrote on 15 Dec 2023, 08:26 last edited by JonB
                #6

                @jsulm
                As I wrote, and the OP is asking, does that not still call QString() converter at start up (or wherever depending on where the line is placed)? Isn't the point of QStringLiteral() (which I have never bothered to use) that it does the work at compile-time instead?

                R J 2 Replies Last reply 15 Dec 2023, 08:30
                0
                • J JonB
                  15 Dec 2023, 08:26

                  @jsulm
                  As I wrote, and the OP is asking, does that not still call QString() converter at start up (or wherever depending on where the line is placed)? Isn't the point of QStringLiteral() (which I have never bothered to use) that it does the work at compile-time instead?

                  R Offline
                  R Offline
                  Redman
                  wrote on 15 Dec 2023, 08:30 last edited by
                  #7

                  @JonB
                  Thats right. Here is a good explanation https://woboq.com/blog/qstringliteral.html

                  J 1 Reply Last reply 15 Dec 2023, 08:34
                  0
                  • R Redman
                    15 Dec 2023, 08:30

                    @JonB
                    Thats right. Here is a good explanation https://woboq.com/blog/qstringliteral.html

                    J Online
                    J Online
                    JonB
                    wrote on 15 Dec 2023, 08:34 last edited by
                    #8

                    @Redman
                    Indeed. But nonetheless my earlier post still stands: yes, use QStringLiteral() here (and everywhere) if you want to squeeze the last drop out of speed, but don't kid yourself that in your case it will make the slightest difference to the UI or anything else.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 15 Dec 2023, 08:52 last edited by
                      #9

                      I'm gonna go a bit against my colleagues here and say go for it. Avoiding dynamic allocation is always a good thing, especially on platforms on which it's a very costly operation (can go in thousands of cycles on some embedded hardware).
                      Even if you don't see much of a difference on your particular platform it's simply a good habit to write code that is performant by default, especially when there's not much difference in code complexity associated with it.

                      C 1 Reply Last reply 15 Dec 2023, 09:22
                      2
                      • J JonB
                        15 Dec 2023, 08:26

                        @jsulm
                        As I wrote, and the OP is asking, does that not still call QString() converter at start up (or wherever depending on where the line is placed)? Isn't the point of QStringLiteral() (which I have never bothered to use) that it does the work at compile-time instead?

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 15 Dec 2023, 08:59 last edited by
                        #10

                        @JonB said in QStringLiteral optimization:

                        does that not still call QString() converter at start up

                        Not since C++11 as far as I know, but not sure about QString. See https://en.cppreference.com/w/cpp/language/list_initialization

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        C 1 Reply Last reply 15 Dec 2023, 09:07
                        0
                        • J jsulm
                          15 Dec 2023, 08:59

                          @JonB said in QStringLiteral optimization:

                          does that not still call QString() converter at start up

                          Not since C++11 as far as I know, but not sure about QString. See https://en.cppreference.com/w/cpp/language/list_initialization

                          C Offline
                          C Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on 15 Dec 2023, 09:07 last edited by
                          #11

                          @jsulm Since QString doesn't have dedicated initializer_list constructor QString tableName{"users"} is the same as QString tableName("users"). It just calls QString(const char *str), which allocates and does conversion via QString::fromUtf8.

                          1 Reply Last reply
                          2
                          • C Chris Kawa
                            15 Dec 2023, 08:52

                            I'm gonna go a bit against my colleagues here and say go for it. Avoiding dynamic allocation is always a good thing, especially on platforms on which it's a very costly operation (can go in thousands of cycles on some embedded hardware).
                            Even if you don't see much of a difference on your particular platform it's simply a good habit to write code that is performant by default, especially when there's not much difference in code complexity associated with it.

                            C Offline
                            C Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 15 Dec 2023, 09:22 last edited by Christian Ehrlicher
                            #12

                            @Chris-Kawa said in QStringLiteral optimization:

                            I'm gonna go a bit against my colleagues here and say go for it.

                            But this was not his question... there will be no startup speedup which is measurable for this single string. You're right that all the tiny things will maybe bring something but then the question was wrong (and in the first incomplete).
                            Yes, you should use QStringLiteral, but this is also written in the documentation: https://doc.qt.io/qt-6/qstring.html#QStringLiteral but there is a lot lot of other stuff which will affect your performance first before seeing any gain due to this change.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            2
                            • R Redman has marked this topic as solved on 15 Dec 2023, 12:55

                            1/12

                            15 Dec 2023, 08:18

                            • Login

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