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 Updated to NodeBB v4.3 + New Features

QStringLiteral optimization

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 708 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 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

    Christian EhrlicherC JonBJ jsulmJ 3 Replies Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 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.

      Christian EhrlicherC 1 Reply Last reply
      2
      • R Redman

        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

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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
        0
        • Christian EhrlicherC Christian Ehrlicher

          @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 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

            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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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

              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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Redman said in QStringLiteral optimization:

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

                Why not

                inline const static QString tableName{"users"};
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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 jsulmJ 2 Replies Last reply
                0
                • JonBJ JonB

                  @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 last edited by
                  #7

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

                  JonBJ 1 Reply Last reply
                  0
                  • R Redman

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 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.

                      Christian EhrlicherC 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @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?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                        Chris KawaC 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @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

                          Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on 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
                          • Chris KawaC Chris Kawa

                            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.

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 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

                            • Login

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