Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple question

Reply
Thread Tools

Simple question

 
 
Mosfet
Guest
Posts: n/a
 
      04-24-2009
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
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      04-24-2009
On 24 Apr., 18:10, Mosfet <mos...@anonymous.org> wrote:
> 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
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      04-24-2009
Mosfet wrote:

> 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...

--
Phlip


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-25-2009
On Apr 24, 6:44 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> Mosfet wrote:


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


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


> 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.

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


> 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.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments