Simple question

M

Mosfet

Hi,

I wanted to know what is pros/cons between this two ways of declaring a
function :


std::string GetCurrentPath();
or
void GetCurrentPath(const std::string& sCurPath);


In terms of rapidity, memory , ...
I supose form 2 is better because there is no copy but is it really
significant ?

Thanks
 
P

peter koch

Hi,

I wanted to know what is pros/cons between this two ways of declaring a
function :

std::string GetCurrentPath();
or
void GetCurrentPath(const std::string& sCurPath);

In terms of rapidity, memory , ...
I supose form 2 is better because there is no copy but is it really
significant ?

In general the first version will be faster. This is the case whenever
the call will be used directly in the constructor. Anyway, it is more
ideomatic and certainly saves lots of programmer-time. It is also very
easy to change from the first version to the second should some inner
loop demonstrate to be faster using the second version.

/Peter
 
P

Phlip

Mosfet said:
std::string GetCurrentPath();
or
void GetCurrentPath(const std::string& sCurPath);

In terms of rapidity, memory , ...

Premature optimization is the root of all evil. You should write what works
best for the programmer - typically the first one.

C++ will optimize the std::string constructor to synchronize with the
constructor inside the GetCurrentPath(). They are the same object - that's
the "return value optimization".

But you should write the simplest and most readable code possible, even C++
cannot optimize it. Even if you would get a few extra copies. And you should
write lots of unit tests. After you have a running application, and you can
actually time-test it to see where the real bottlenecks are. They are most
likely not something you would have guessed. These practices free your time
up to focus directly on the parts of your code that actually need the
performance boosts...
 
J

James Kanze

Mosfet wrote:
Those aren't equivalent. Form 1 takes no inputs but has one
output, but vice versa for form 2. If you meant to pass the
string by non-const reference, then I prefer the former,
since:
- the latter requires the string to be non-const in the
caller, and
- the latter can only be used for assignment, not
initialization, whereas the former can be used for either.

As an extension to what you've said: you can't use the second in
expressions. Things like getCurrentPath() + '/', for example.
Which is, IMHO, a killer argument.

It's worth noting, too, that C++ has absolutely no support for
out parameters. For in parameters, you can use pass by value or
const reference. For inout, pass by non const reference, but
for out... You're still required to provide an in value, even
if the called function doesn't care.
There's not necessarily any copy in form 1, either. The
compiler is allowed to elide the copy. Form 2 can actually
prevent some optimizations, since GetCurrentPath might cache a
pointer to the caller's string.

Not to mention the fact that you've got to construct a string
before calling the function. Constructing a string immediately,
to return it, could be quicker constructing it, then assigning a
value to it.

But of course, the argument is misplaced. It's hard to imagine
a case where the difference would be measurable, and when such a
case occurs, that's the time to fix it. Speculatively fixing
supposed performance problems before they exist is just stupid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top