CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: would ever passing a primitive to functions by reference be more effecient ?

  1. #1
    Join Date
    Oct 2010

    would ever passing a primitive to functions by reference be more effecient ?

    In a scenario that a caller function needs to pass one of its local variables of a primitive type to a callee and doesn't necessarily need to pass it by reference to track that variable later after callee is done, I know that it'd be said to pass it by value and that makes total sense for primitive types with a size equal to or smaller than size of pointer; but I'm curious that from an assembly point(performance-wise or space-wise), wouldn't be there a situation when passing a variable of a primitive type with a size bigger than pointers like long double on my platform which has a size of 8 bytes and is bigger than pointers that have a size of 4 bytes; by reference would be more efficient? like an imagined situation where pointer can be loaded directly into some register by caller but the primitive itself not and thus no need to load the pointer from callee stack frame to some register by callee and there's 8 more free bytes of stack memory in the end comparing to pass by value where there'd be 8 more used bytes of stack memory.
    If in this specific case, passing by reference might ever be more efficient, how can we know to pass by reference or value?

  2. #2
    Join Date
    Jun 2009

    Re: would ever passing a primitive to functions by reference be more effecient ?

    Stack use has never really been an issue, you are very rarely short on stack space, so the point has never been to save bytes.

    The real trade of is between dereferencing a pointer (when passing by reference), and the cost of the copy.

    Notice that I said cost of the copy. This is important. For primitive types and simple POD structs, the only cost is to copy on the stack. I think you can easily go up to 3-4 integer (12-16 bytes) before going for references.

    Now imagine a vector. A vector is actually only 12 bytes on the stack (!) However, copying a vector also implies heap allocations, and data assignment. VERY costly.

    Don't assume it is only a tradeof of stack space. It is a tradeof of copy cost in terms of time.

    To answer your question, there is no single rule, just profile your code and see which is better.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    May 2009

    Re: would ever passing a primitive to functions by reference be more effecient ?

    Quote Originally Posted by Garrett View Post
    , how can we know to pass by reference or value?
    In my view this is the kind of premature optimizations you shouldn't bother with.

    There are too many unknown factors you don't control and the gain if you manage to find some optimal parameter usage strategy is minimal. And it's brittle. Say you change compilers or some compiler option, or say you port your program from 32 to 64 bit then your carefully tuned parameter strategy may be totally off.

    Instead use the simple rule of thumb to call primitives by value and everything else by const reference. In addition put functions with tiny bodies (typically one line) in the class declaration so they get inlined.

    Only consider function call overhead when,

    a) the time spent calling a function is substantial in relation to the time spent doing actual work in the function body, and
    b) you know the function is part of a performance bottleneck because you've measured and established that, and
    c) you've established that there isn't a more efficient algorithm available.
    Last edited by nuzzle; October 12th, 2010 at 03:44 PM.

  4. #4
    Join Date
    Jan 2009

    Re: would ever passing a primitive to functions by reference be more effecient ?

    First off, pointers are not guaranteed to be 4 bytes. Unless your code is specifically designed to run an a legacy system assume that your pointers will be 64-bit. I'm fairly certain 64-bit is the majority now, and the percentage is going up fast, so unless its a 96-bit or 128-bit primitive, you save no space.

    Compilers are also getting really smart, and might do things like that for you, or inline code, or even compile the entire variable out into a register that never even needs to be pushed. Registers are 10,000 times faster than RAM, and compilers know that.

    You can always do a test, write a method that passes by reference and one that passes by value, call them each a few millions times and profile them. I'm betting there will be little or no difference. As nuzzle said, programs that are slow are that way because of poor engineering, not method calling types (unless the engineer did something REALLY dumb.)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)