Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. compiler optimization
Forum Updated to NodeBB v4.3 + New Features

compiler optimization

Scheduled Pinned Locked Moved Solved C++ Gurus
8 Posts 5 Posters 880 Views 3 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.
  • D Offline
    D Offline
    damianatorrpm
    wrote on last edited by
    #1

    Hi,

    I was wondering if the following statement makes any difference in amount of executed code:
    a) myQString.remove("something");
    b) myQString = myQString.remove("something");
    same for append prepend etc.

    wouldn't b) be on more instruction? If yes do recent compilers "fix" this?
    The syntax of b) is IMHO much clearer and I prefer it, but it isn't obvious to me and it seems different from
    i = i+1 or i +=1 as a function => .remove/.append etc is used.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

      Extra instructions:

      mov     rsi, rsp
      mov     rdi, rsp
      call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      D kshegunovK sierdzioS 3 Replies Last reply
      6
      • VRoninV VRonin

        I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

        Extra instructions:

        mov     rsi, rsp
        mov     rdi, rsp
        call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
        
        D Offline
        D Offline
        damianatorrpm
        wrote on last edited by
        #3

        @VRonin Thank you for the answer.
        I will mark the topic solved later today hoping someone miraculously says but moc does this or something.
        Anyways I will fiddle with the spacing/linebreaks of that code to like option a) more...

        Thanks again.

        aha_1980A 1 Reply Last reply
        0
        • D damianatorrpm

          @VRonin Thank you for the answer.
          I will mark the topic solved later today hoping someone miraculously says but moc does this or something.
          Anyways I will fiddle with the spacing/linebreaks of that code to like option a) more...

          Thanks again.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by aha_1980
          #4

          Hi @damianatorrpm,

          QString::remove modifies the object it is called on. Don't fool yourself by writing:

          myQString = myQString.remove("something");

          because you might be tempted to write:

          const QString new = old.remove("foo");

          also, which modifies old, but that might not be obvious by looking at the code.

          Regards

          Qt has to stay free or it will die.

          D 1 Reply Last reply
          5
          • aha_1980A aha_1980

            Hi @damianatorrpm,

            QString::remove modifies the object it is called on. Don't fool yourself by writing:

            myQString = myQString.remove("something");

            because you might be tempted to write:

            const QString new = old.remove("foo");

            also, which modifies old, but that might not be obvious by looking at the code.

            Regards

            D Offline
            D Offline
            damianatorrpm
            wrote on last edited by damianatorrpm
            #5

            @aha_1980

            because you might be tempted to write:
            const QString new = old.remove("foo");

            This is basically the reason I started b), long story, but I have to refactor that part where I started b) anyways, fewer_instruction.append("are fewer instructions").

            Thanks everyone.

            EDIT: I think I will switch to the operator type appending where possible to give contrast to functions that edit the data in place.

            1 Reply Last reply
            1
            • VRoninV VRonin

              I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

              Extra instructions:

              mov     rsi, rsp
              mov     rdi, rsp
              call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @VRonin said in compiler optimization:

              I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

              What's the code you checked with and what optimization level? std::string and its slave classes are inline, so the compiler sees it all and should've inlined the call at the very least (if not using -O0).

              Edit: iirc recent standards made RVO mandatory.

              @damianatorrpm said in compiler optimization:

              I was wondering if the following statement makes any difference in amount of executed code

              It's more complicated than that; the number of instructions is not a direct measure. Instructions are not created equal, and there are specifics that are more important than the actual instruction (like branching and data colocation). On that topic, have you proven that this piece is a bottleneck to begin with? If you haven't, don't try to microoptimize it preemtively.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              5
              • VRoninV VRonin

                I checked the compiler output with g++ (and using std::string instead of QString) and it looks like the assignment is not optimised out so syntax a) is better.

                Extra instructions:

                mov     rsi, rsp
                mov     rdi, rsp
                call    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
                
                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by sierdzio
                #7

                @VRonin said in compiler optimization:

                I checked the compiler output with g++ (and using std::string instead of QString)

                That might give a bit misleading results because QString is implicitly shared (copy on write) and std::string is not. I have no idea if it matters in this case, though.

                (Z(:^

                kshegunovK 1 Reply Last reply
                1
                • sierdzioS sierdzio

                  @VRonin said in compiler optimization:

                  I checked the compiler output with g++ (and using std::string instead of QString)

                  That might give a bit misleading results because QString is implicitly shared (copy on write) and std::string is not. I have no idea if it matters in this case, though.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @sierdzio said in compiler optimization:

                  I have no idea if it matters in this case, though.

                  A great deal. QString has an additional indirection through the d_ptr and this is the great wall of china standing in front of the optimizations the compiler could possibly make.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1

                  • Login

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