Return speed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    Return speed

    If I am going to be using the same class over and over again passing between
    functions, would the following be quicker (as the constructor wouldn't have
    to be called):

    //First Example:

    class MVector{
    float x,y,z;
    MVector() {x=y=z=0);
    };

    class MBoundingBox
    {
    MVector GetCentreVector () {/* Gets Vector */}
    };

    MVector vec;
    MBoundingBox BB[1000];


    for(int i=0;i<10000;i++ )
    {
    vec = BB.GetCentreVec tor[i]();
    /* do something */
    }






    //Second Example:

    class MVector{
    float x,y,z;
    MVector() {x=y=z=0);
    };

    class MBoundingBox
    {
    void GetCentreVector (MVector& vec) { /* put vec into reference */}
    };

    MVector vec;
    MBoundingBox BB[1000];


    for(int i=0;i<10000;i++ )
    {
    BB.GetCentreVec tor[i](vec);
    /* do something */
    }



    ie is it quicker to pass by reference than to return by value??
    Regards

    Michael


  • Victor Bazarov

    #2
    Re: Return speed

    "Michael" <slick_mick_00@ hotmail.com> wrote...[color=blue]
    > If I am going to be using the same class over and over again passing[/color]
    between[color=blue]
    > functions, would the following be quicker (as the constructor wouldn't[/color]
    have[color=blue]
    > to be called):
    >
    > //First Example:
    >
    > class MVector{
    > float x,y,z;[/color]


    public:
    [color=blue]
    > MVector() {x=y=z=0);[/color]

    MVector() : x(0), y(0), z(0) {}
    [color=blue]
    > };
    >
    > class MBoundingBox
    > {[/color]

    public:
    [color=blue]
    > MVector GetCentreVector () {/* Gets Vector */}
    > };
    >
    > MVector vec;
    > MBoundingBox BB[1000];
    >
    >
    > for(int i=0;i<10000;i++ )
    > {
    > vec = BB.GetCentreVec tor[i]();[/color]

    vec = BB[i].GetCentreVecto r();
    [color=blue]
    > /* do something */
    > }
    >
    >
    >
    >
    >
    >
    > //Second Example:
    >
    > class MVector{
    > float x,y,z;[/color]


    public:
    [color=blue]
    > MVector() {x=y=z=0);[/color]

    MVector() : x(0), y(0), z(0) {}
    [color=blue]
    > };
    >
    > class MBoundingBox
    > {[/color]

    public:
    [color=blue]
    > void GetCentreVector (MVector& vec) { /* put vec into reference */}
    > };
    >
    > MVector vec;
    > MBoundingBox BB[1000];
    >
    >
    > for(int i=0;i<10000;i++ )
    > {
    > BB.GetCentreVec tor[i](vec);[/color]

    BB[i].GetCentreVecto r(vec);
    [color=blue]
    > /* do something */
    > }
    >
    >
    >
    > ie is it quicker to pass by reference than to return by value??[/color]

    Usually, yes. In your particular case probably the same, the compiler
    is allowed to do what's known as "return value optimization". Look it
    up.

    Victor


    Comment

    • Iulian Dragos

      #3
      Re: Return speed

      [color=blue]
      > class MVector{
      > float x,y,z;
      > MVector() {x=y=z=0);
      > };
      >
      > class MBoundingBox
      > {
      > MVector GetCentreVector () {/* Gets Vector */}
      > };
      >
      > MVector vec;
      > MBoundingBox BB[1000];
      >
      >
      > for(int i=0;i<10000;i++ )
      > {
      > vec = BB.GetCentreVec tor[i]();
      > /* do something */
      > }[/color]

      Maybe "vec = BB[i].GetCentreVecto r()" ?

      [....][color=blue]
      > ie is it quicker to pass by reference than to return by value??
      > Regards[/color]

      I guess it depends on your compiler.. but you have both programs! Why don't
      you time them and see for yourself? It would be interesting to post the
      results afterwards.

      I would say (based on my limited compiler construction knowledge) that
      passing the reference is quicker: less memory operations and might even be
      passed in a register. I'm not sure whether you can use the reference as an
      "out" parameter, but you can certainly return a reference (like in the first
      example).

      As a side note, in your first example the (default) copy constructor IS
      called.

      A second side note: as you mentioned, you need to pass these structures a
      lot between various functions. Why not make them member functions in your
      structures?

      HTH,
      iuli


      Comment

      • John Harrison

        #4
        Re: Return speed


        "Michael" <slick_mick_00@ hotmail.com> wrote in message
        news:c889en$gm3 $1@titan.btinte rnet.com...[color=blue]
        > If I am going to be using the same class over and over again passing[/color]
        between[color=blue]
        > functions, would the following be quicker (as the constructor wouldn't[/color]
        have[color=blue]
        > to be called):
        >[/color]

        [snip]
        [color=blue]
        >
        > ie is it quicker to pass by reference than to return by value??
        > Regards
        >[/color]

        The C++ language does not specify the speed of any operations. The answer to
        your question could easily depend on which compiler and which machine. The
        ONLY way to answer questions like this is to try both methods and time the
        results.

        john


        Comment

        • E. Robert Tisdale

          #5
          Re: Return speed

          Michael wrote:
          [color=blue]
          > If I am going to be using the same class over and over again passing between
          > functions, would the following be quicker
          > (as the constructor wouldn't have to be called):
          >
          > //First Example:
          >
          > class MVector{[/color]
          private:
          // representation[color=blue]
          > float x, y, z;[/color]
          public:
          // constructors
          MVector(void): x(), y(), z() { }[color=blue]
          > };
          >
          > class MBoundingBox {
          > MVector GetCentreVector (void) {/* Gets Vector */}
          > };
          >
          > MVector vec;
          > MBoundingBox BB[1000];
          >
          > for(int i = 0; i < 10000; ++i) {
          > vec = BB[i].GetCentreVecto r();
          > /* do something */
          > }
          >
          > //Second Example:
          >
          > class MVector {[/color]
          private:
          // representation[color=blue]
          > float x,y,z;[/color]
          public:
          // constructors
          MVector(void): x(), y(), z() { }[color=blue]
          > };
          >
          > class MBoundingBox {[/color]
          public:[color=blue]
          > void GetCentreVector (MVector& vec) { /* put vec into reference */}
          > };
          >
          > MVector vec;
          > MBoundingBox BB[1000];
          >
          >
          > for(int i = 0; i < 10000; ++i) {
          > BB.GetCentreVec tor[i](vec);
          > /* do something */
          > }
          >
          > i.e. Is it quicker to pass by reference than to return by value?[/color]

          No.

          Comment

          Working...