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. Loops, Constructors and Performance
Forum Updated to NodeBB v4.3 + New Features

Loops, Constructors and Performance

Scheduled Pinned Locked Moved Solved C++ Gurus
19 Posts 9 Posters 3.5k Views 4 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.
  • Kent-DorfmanK Kent-Dorfman
    while (...) {
        ...
        m_surface.Ev1Der(u, v, on_p, on_du1, on_dv1);
    
        QVector3D p = QVector3D(on_p.x, on_p.y, on_p.z);
        QVector3D duu = QVector3D(on_du1.x, on_du1.y, on_du1.z);
        QVector3D dvv = QVector3D(on_dv1.x, on_dv1.y, on_dv1.z);
        ...
    }
    

    This is the more C++ way of doing it because it limits the scope of the objects to where they are used. You can drive yourself nuts worrying about premature optimization issues of what's more efficient so that's not as big an initial concern.

    One change I do recommend going forward is that instead of using the

    Ojbect o = Object(a,b,c) form you should use the C++ way of initializing object as:

    Object o(a,b,c) This syntax has the advantage that it is explicitly running the constructor only. The previous syntaxt may do both: construct the object into a temporary and then assign it to the destination object: 2 operations!

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #7

    @Kent-Dorfman said in Loops, Constructors and Performance:

    Ojbect o = Object(a,b,c) form you should use the C++ way of initializing object as:
    Object o(a,b,c)

    Minor note auto o = Object(a,b,c) is also acceptable

    "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

    K 1 Reply Last reply
    1
    • beeckscheB beecksche

      Hi,
      code is worth a thousand words ;-)
      I have this loop in my class:

      ON_3dPoint on_p;
      ON_3dVector on_du1;
      ON_3dVector on_dv1;
      
      QVector3D p;
      QVector3D duu;
      QVector3D dvv;
      
      while (...) {
          ...
          m_surface.Ev1Der(u, v, on_p, on_du1, on_dv1);
      
          p.setX(on_p.x);
          p.setY(on_p.y);
          p.setZ(on_p.z);
      
          duu.setX(on_du1.x);
          duu.setY(on_du1.y);
          duu.setZ(on_du1.z);
      
          dvv.setX(on_dv1.x);
          dvv.setY(on_dv1.y);
          dvv.setZ(on_dv1.z);
          ...
      }
      

      Is it better to use the setter function of QVector3D or to put the objects p, duu and dvv into the while loop:

      ON_3dPoint on_p;
      ON_3dVector on_du1;
      ON_3dVector on_dv1;
      
      while (...) {
          ...
          m_surface.Ev1Der(u, v, on_p, on_du1, on_dv1);
      
          QVector3D p = QVector3D(on_p.x, on_p.y, on_p.z);
          QVector3D duu = QVector3D(on_du1.x, on_du1.y, on_du1.z);
          QVector3D dvv = QVector3D(on_dv1.x, on_dv1.y, on_dv1.z);
          ...
      }
      

      To read the code I would prefer the second way. But I think that I read something about, that you should create a object outside of a loop.

      Do you know some advantages or disadvantages of this? I'm working with the MSVC 2017 compiler (if this is necessary)
      Thanks a lot!

      K Offline
      K Offline
      Konstantin Tokarev
      wrote on last edited by
      #8

      @beecksche said in Loops, Constructors and Performance:

      But I think that I read something about, that you should create a object outside of a loop.

      In this case it doesn't matter, because you overwrite all vector elements on each iteration, and constructor of QVector3D doesn't make any dynamic allocations (QVector3D is actually float[3] with fancy API around)

      1 Reply Last reply
      1
      • VRoninV VRonin

        @Kent-Dorfman said in Loops, Constructors and Performance:

        Ojbect o = Object(a,b,c) form you should use the C++ way of initializing object as:
        Object o(a,b,c)

        Minor note auto o = Object(a,b,c) is also acceptable

        K Offline
        K Offline
        Konstantin Tokarev
        wrote on last edited by
        #9

        @VRonin said in Loops, Constructors and Performance:

        Minor note auto o = Object(a,b,c) is also acceptable

        Why do this when just QVector3D p(a, b, c) can work?

        J.HilkJ VRoninV 2 Replies Last reply
        1
        • K Konstantin Tokarev

          @VRonin said in Loops, Constructors and Performance:

          Minor note auto o = Object(a,b,c) is also acceptable

          Why do this when just QVector3D p(a, b, c) can work?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #10

          @Konstantin-Tokarev said in Loops, Constructors and Performance:

          @VRonin said in Loops, Constructors and Performance:

          Minor note auto o = Object(a,b,c) is also acceptable

          Why do this when just QVector3D p(a, b, c) can work?

          Why use auto at all... It looks more modern and fancy.

          I'm with Bjarne Stroustrup and wish auto would have never been added to the standard.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          Kent-DorfmanK 1 Reply Last reply
          2
          • K Konstantin Tokarev

            @VRonin said in Loops, Constructors and Performance:

            Minor note auto o = Object(a,b,c) is also acceptable

            Why do this when just QVector3D p(a, b, c) can work?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #11

            @Konstantin-Tokarev said in Loops, Constructors and Performance:

            Why do this when just QVector3D p(a, b, c) can work?

            Linking ≠ Endorsement
            https://www.youtube.com/watch?v=xnqTKD8uD64&t=40m00s

            "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

            kshegunovK 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Konstantin-Tokarev said in Loops, Constructors and Performance:

              @VRonin said in Loops, Constructors and Performance:

              Minor note auto o = Object(a,b,c) is also acceptable

              Why do this when just QVector3D p(a, b, c) can work?

              Why use auto at all... It looks more modern and fancy.

              I'm with Bjarne Stroustrup and wish auto would have never been added to the standard.

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by Kent-Dorfman
              #12

              @J.Hilk said in Loops, Constructors and Performance:

              @Konstantin-Tokarev said in Loops, Constructors and Performance:

              @VRonin said in Loops, Constructors and Performance:

              Minor note auto o = Object(a,b,c) is also acceptable

              Why do this when just QVector3D p(a, b, c) can work?

              Why use auto at all... It looks more modern and fancy.

              I'm with Bjarne Stroustrup and wish auto would have never been added to the standard.

              Actually I think Bjarne was misrepresented on that one. When I read his review of c++11, he seemed to at least be "politicially positive" about the new features...and really, auto is comes into its own when using long winded iterator declarations.

              for(auto i: container) {} is so much more elegant than manually declaring the iterator type

              but many of these arguements are just esoteric/philosophical rants to exercise and justify our OCD. Programmers with OCD?...yeah, right!

              1 Reply Last reply
              1
              • VRoninV VRonin

                @Konstantin-Tokarev said in Loops, Constructors and Performance:

                Why do this when just QVector3D p(a, b, c) can work?

                Linking ≠ Endorsement
                https://www.youtube.com/watch?v=xnqTKD8uD64&t=40m00s

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

                @VRonin
                How is

                auto o = Object(a, b, c);
                

                different from:

                Object o = Object(a, b, c);
                

                for the generated code?

                @Kent-Dorfman
                In my mind auto was invented for the template junkies out there. The only real reason to actually "need" it in non-(heavily-)templated code is when doing ranged loops with arrays of anonymous structures. Something like this:

                static struct  {
                   int member1;
                   // ....
                } anonymousGlobal[] =  {
                  { 0, /* ... */ }
                };
                
                for (auto x : anonymousGlobal)
                    // ...
                

                And even then I'd put some question on the clarity of such code.

                Read and abide by the Qt Code of Conduct

                Kent-DorfmanK VRoninV 2 Replies Last reply
                1
                • kshegunovK kshegunov

                  @VRonin
                  How is

                  auto o = Object(a, b, c);
                  

                  different from:

                  Object o = Object(a, b, c);
                  

                  for the generated code?

                  @Kent-Dorfman
                  In my mind auto was invented for the template junkies out there. The only real reason to actually "need" it in non-(heavily-)templated code is when doing ranged loops with arrays of anonymous structures. Something like this:

                  static struct  {
                     int member1;
                     // ....
                  } anonymousGlobal[] =  {
                    { 0, /* ... */ }
                  };
                  
                  for (auto x : anonymousGlobal)
                      // ...
                  

                  And even then I'd put some question on the clarity of such code.

                  Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #14

                  @Kent-Dorfman

                  In my mind auto was invented for the template junkies out there. The only real reason to actually "need" it in non-(heavily-)templated code is when doing ranged loops with arrays of anonymous structures. Something like this:

                  static struct  {
                     int member1;
                     // ....
                  } anonymousGlobal[] =  {
                    { 0, /* ... */ }
                  };
                  
                  for (auto x : anonymousGlobal)
                      // ...
                  

                  And even then I'd put some question on the clarity of such code.

                  fair enough...too often features become peoples religion and they go out of their way to overuse or justify their use. I personally think auto is a good idea, for specifically the reasons specified...it is a good shorthand for complex type declarations and I hate being long-winded, although you usually couldn't tell that from my written correspondence. I've been in python land for a while now and am less than anxious to see all the overuse of lambdas that have undoubtedly sprung up now that they are in the c++ spec. lambdas are my soap-box rant.

                  1 Reply Last reply
                  1
                  • kshegunovK kshegunov

                    @VRonin
                    How is

                    auto o = Object(a, b, c);
                    

                    different from:

                    Object o = Object(a, b, c);
                    

                    for the generated code?

                    @Kent-Dorfman
                    In my mind auto was invented for the template junkies out there. The only real reason to actually "need" it in non-(heavily-)templated code is when doing ranged loops with arrays of anonymous structures. Something like this:

                    static struct  {
                       int member1;
                       // ....
                    } anonymousGlobal[] =  {
                      { 0, /* ... */ }
                    };
                    
                    for (auto x : anonymousGlobal)
                        // ...
                    

                    And even then I'd put some question on the clarity of such code.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #15

                    @kshegunov said in Loops, Constructors and Performance:

                    How is
                    auto o = Object(a, b, c);
                    different from:
                    Object o = Object(a, b, c);

                    Actually all 3 forms are the same, the compiler (MSVC2015 and GCC 4.9 at least) takes care of doing the right thing:

                    #include <QDebug>
                    class Object{
                    public:
                        Object(int){qDebug("Constructor");}
                        Object(const Object&){qDebug("Copy Constructor");}
                        Object(Object&&){qDebug("Move Constructor");}
                    };
                    
                    int main(int argc, char ** argv)
                    {
                       Object a(6);
                       qDebug("############");
                       Object b = Object(6);
                       qDebug("############");
                       auto c = Object(6);
                       qDebug("############");
                       return 0;
                    }
                    

                    Outputs

                    Constructor
                    ############
                    Constructor
                    ############
                    Constructor
                    ############
                    

                    So we are worrying about a non-problem

                    "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

                    1 Reply Last reply
                    4
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @Kent-Dorfman said in Loops, Constructors and Performance:

                      Object o(a,b,c) This syntax has the advantage that it is explicitly running the constructor only. The previous syntaxt may do both: construct the object into a temporary and then assign it to the destination object: 2 operations!

                      I would accept this answer 10 years ago but not nowadays and with c++14 it's wrong at all:
                      https://en.wikipedia.org/wiki/Copy_elision and https://en.cppreference.com/w/cpp/language/copy_elision

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

                      kshegunovK 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        @Kent-Dorfman said in Loops, Constructors and Performance:

                        Object o(a,b,c) This syntax has the advantage that it is explicitly running the constructor only. The previous syntaxt may do both: construct the object into a temporary and then assign it to the destination object: 2 operations!

                        I would accept this answer 10 years ago but not nowadays and with c++14 it's wrong at all:
                        https://en.wikipedia.org/wiki/Copy_elision and https://en.cppreference.com/w/cpp/language/copy_elision

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

                        @Christian-Ehrlicher said in Loops, Constructors and Performance:

                        I would accept this answer 10 years ago but not nowadays and with c++14 it's wrong at all

                        Maybe in a perfect world. The compiler I use on the cluster has c++11 and partial c++14 support. Old, but it's out of my hands. Considerations to such contexts apply.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #18

                          gcc has '-fno-elide-constructors' since 4.0 so what ancient compiler do you use? ;)

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

                          kshegunovK 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            gcc has '-fno-elide-constructors' since 4.0 so what ancient compiler do you use? ;)

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

                            This disables eliding - the optimization you're after. So the expression:

                            Object a = Object(x, y, z);
                            

                            is going to always invoke the copy constructor with that flag. On the other hand -felide-constructors may or may not generate a constructor call depending on the specific version and optimizations the compiler does.

                            Read and abide by the Qt Code of Conduct

                            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