locked
Reference pass speed question RRS feed

  • Question

  • Suppose I have an overloaded function that takes references to an int, a vector of ints, and a vector of vectors of ints.  For example, take the declarations to be:

    void MyFunction(int& parameter);
    
    void MyFunction(vector<int>& parameter);
    
    void MyFunction(vector< vector<int> >& parameter);
    
    My question is how do these different passed references affect execution time?  That is does the vector< vector<int> > reference take longer to pass than the vector<int> which takes longer than the int, or do they all take the same amount of time and execution time is unaffected?
    Friday, September 24, 2010 12:54 PM

Answers

  • Executiontime is unaffected, because you pass a reference, which have pointer-size.

    It would be different, if you would pass the object itself, because then, the object must be copied, and copying would cost time.

    • Proposed as answer by Spencer Putt Friday, September 24, 2010 1:38 PM
    • Marked as answer by digiplant Friday, September 24, 2010 1:40 PM
    Friday, September 24, 2010 1:36 PM

All replies

  • Executiontime is unaffected, because you pass a reference, which have pointer-size.

    It would be different, if you would pass the object itself, because then, the object must be copied, and copying would cost time.

    • Proposed as answer by Spencer Putt Friday, September 24, 2010 1:38 PM
    • Marked as answer by digiplant Friday, September 24, 2010 1:40 PM
    Friday, September 24, 2010 1:36 PM
  • >My question is how do these different passed references affect execution time?  That is does the vector< vector<int> > reference take longer to pass than the vector<int> which takes longer than the int, or do they all take the same amount of time and execution time is unaffected?

    They're identical - what's passed is a reference, which is essentially
    a pointer in all situations.

    Dave

    Friday, September 24, 2010 1:41 PM
  • That is does the vector< vector<int> > reference take longer to pass han the vector<int> which takes longer than the int, or do they all take the same amount of time and execution time is unaffected?
    Both take same time, exactly 4 byte or 8 bytes to push on stack, depending on 32/64-bit compilation. Reference and pointers need word-size of platform.
    Sunday, September 26, 2010 7:01 AM