The efficiency of return values


Wouldn't it be more efficient to just directly modify the variable by passing it by reference??

Every time we call a function, it will allocate new space for the return variable. Since the return value is temporary, it will also be destroyed. The construction and destruction of the new variable does present more work for the program.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<string> getsomthing()
{
    vector<string> v;
    for(unsigned int x = 0; x < 100; x++)
    {
        v.push_back(conv<uunsigned int, string>(x)); //add the number...
    }
    return v;
}

//would this not be more efficient??
void getsomthing(vector<string>& v)
{
    for(unsigned int x = 0; x < 100; x++)
    {
        v.push_back(conv<unsigned int, string>(x));
    }
}


Last edited on
Maybe, but not necessarily.

- Passing by reference often adds an extra level of indirecton (it's basically a pointer) which is an additional memory access each time you access the object.

- Copy and destruction of returned values are often optimized away by the compiler.

- Even when they're not, C++11 move constructors are very quick at moving a vector.



In short: don't worry about it. Leave this kind of micro-optimizing to the compiler.
closed account (S6k9GNh0)
In a lot of C APIs, when you allocate objects, a pointer is returned to the allocated object. When you call a function that modifies the object, you pass the pointer so it can be modified directly (or if the object is const, so it can be read directly).

You generally never want to pass something larger than pointer size by value. While indirection can be a performance hit, it can be optimized away from the compiler (of course, depending well on use case).

Of course in C++, this is sometimes hard to tell since there are various levels of abstraction where its sometimes difficult to tell what's going on underneath. For instance, unique_ptr has /zero/ overhead when in use which is sometimes difficult to grasp in concept.

It's all about what knowing what your compiler can optimize away, knowing how the standard dictates a facility should be handled, and making sure to read the manual book on whatever library (including the STL) that you're using.
Last edited on
@IWishIKnew

The second (reference parameter) version of getsomething() you give is the way I was taught as a junior developer last millenium, but the rules have changed since then. And are in the process of changing again, thanks to C++11.

This article by Dave Abrahams discusses this very issue in some detail:

Want Speed? Pass by Value.
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

Andy

PS Similar earlier thread...

preferable way to return a vector?
http://www.cplusplus.com/forum/general/56177/
Last edited on
You generally want to return the C++ containers by value (unless the purpose of the function is to grow or modify in-place an existing container, in which case it's accessed by reference).
uhg... I wish it would be what it is... and if that were true, then I would be right...

Always with the compiler... I'm sure good ol' mel would just write it in hex. ;) LOL.
Topic archived. No new replies allowed.