Thread: passing std::vector and optimisation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    passing std::vector and optimisation

    The current code i have right now:

    Code:
    class main {
    public:
     std::vector<std::string> &get_vector() { return m_vector; }
     const std::vector<std::string> &get_vector() const { return m_vector; }
    
    private:
     std::vector<std::string> m_vector;
    };
    
    
    class subclass {
    private:
     main &m_main;
     void somefunction() {
    	std::vector<std::string>::iterator from = m_main.get_vector().end() - 2;
    	callsomefunc(std::vector<std::string>(from, m_main.get_vector().end()) ); //call somefunc and pass vector with last 2 elements
     }
    };

    I want to redesign it to:


    Code:
    class main {
    public:
     std::vector<std::string> get_last_elements() { return std::vector<std::string>(m_vector.end() - 2, m_vector.end() ); }
    
    private:
     std::vector<std::string> m_vector;
    };
    
    class subclass {
    private:
     main &m_main;
     void somefunction() {
    	callsomefunc(m_main.get_last_elements());
     }
    };
    but I have some preformance considerations on second design.. Will it be slower because of passing value directly instead of reference?
    Or will my compiler optimise that? Which design should I go for if theres a lot of copying in my application?

    Many thanks in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Yes, it's slower because it will create a temporary object, invoke copy constructor and return, then create a new object, invoke copy constructor again and finally invoke destructor and destroy temporary object. Can the compiler optimize it? Yes, it can.
    2) You want to make sure the compiler supports the optimization or go with the reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by l2u View Post
    Which design should I go for if theres a lot of copying in my application?
    A design that doesn't copy around private members all over the place. In short, prefer...

    foo.doWork();

    ...to...

    baz b = foo.getB();
    bah h = foo.getH();
    // Do something with b and h to create r
    foo.setResult(r);

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The second version seems somewhat better. At least subclass doesn't need to know that its the last two items in the main vector that are special.

    If this really is bad for performance, then I guess working through iterators might be more effective.

    But the main question is: cannot main implement somefunction instead of subclass (so it won't need to give out its vector or part of it at all)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Wait... You have a class called "main"??? No, now that's... That's just wrong... Evil and wrong. Does it actually compile?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Wait... You have a class called "main"??? No, now that's... That's just wrong... Evil and wrong. Does it actually compile?
    It is allowed if the main class is not in the global namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by cpjust View Post
    Wait... You have a class called "main"??? No, now that's... That's just wrong... Evil and wrong. Does it actually compile?
    Forget about main class, I only called it main for this example.

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I would think a better design might look something like:

    Code:
    class vecClass{
    
    public:
    
      //...
    
    private:
    
      std::vector<whatever> mVector;
    };
    
    void someFunction( std::vector<whatever>& aVector ){
    
      // ... Do whatever
    }
    
    int main(){
    
      vecClass mVec;
    
      // ...
    
      someFunction( mVec );
    
      return 0;
    }
    But hey, that's just me...
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would think a better design might look something like:
    someFunction() is declared as taking a std::vector<whatever> as its argument, but you pass a vecClass to it in main(). In other words, someFunction() may need to be a friend, but if so then perhaps it could just as well be a member.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Oh yea, typed a little too fast w/out thinking, I meant for it to take a ( vecClass& aVecClass ); Pretend it was that.

    so look like this:
    Code:
    someFunction( vecClass& aVecClass ){
    
      // If getLasTwo() is a member function then:
    
      std::vector<std::string> anotherVec = aVecClass.getLastTwo();
    
    }
    I don't know your design requirements, but I prefer the second way. I mean, why bother having a class at all in your first design?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the thing that is more under question is whether the getLastTwo function is effective or needed in the first place. Perhaps the OP has some reason to store a reference to the other class rather than pass the reference to only SomeFunction (e.g the referred object has to remain the same for this instance).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM