f(int& i) vs. f(int i)

J

jeffc

Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool, etc.)
 
G

gabriel

jeffc said:
Can anyone point me to a web site that gives technical reasons that
pass by value is preferred over pass by reference when the value is
not to be changed? Actually it could be any built-in "small" type
(short, bool, etc.)

Technical reasons? I guess there are none... I mean, if you pass a "small
type," as you call it, you are passiing either the value itself on the
stack (if pass by value), or you are passing a pointer to it (if pass by
reference), both of which are probably the same size (ie, a pointer might
be 32 bits, and an integer value might be the same size).

Non-technical reasons? Well, if you pass by value you cannot change the
value within the routine for sure, and of course, the discussion can get
much more involved on the non-technical aspects (ie, const, etc...).
 
G

Gianni Mariani

jeffc said:
Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool, etc.)

Let's see:

int pass_by_reference( int & a, int & b )
{
return a + b;
}


int pass_by_value( int a, int b )
{
return a + b;
}


passing_params.o: file format elf32-i386

Disassembly of section .text:

00000000 <_Z17pass_by_referenceRiS_>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 8b 00 mov (%eax),%eax
8: 8b 55 08 mov 0x8(%ebp),%edx
b: 03 02 add (%edx),%eax
d: c9 leave
e: c3 ret
f: 90 nop

00000010 <_Z13pass_by_valueii>:
10: 55 push %ebp
11: 89 e5 mov %esp,%ebp
13: 8b 45 0c mov 0xc(%ebp),%eax
16: 03 45 08 add 0x8(%ebp),%eax
19: c9 leave
1a: c3 ret

Which one do you think is faster ?

Try a passing parameters in registers calling convention (SPARC).

_Z17pass_by_referenceRiS_:
ld [%o0], %g1
ld [%o1], %o2
retl
add %g1, %o2, %o0


_Z13pass_by_valueii:
retl
add %o0, %o1, %o0

Now which one do you think is faster ?

The true answer is - it depends. A simple function like these are
easily candidates for inlining - however if these are not inlineable
then passing somthing that can reside in a register by value rather than
by reference can have performance gains due to not needing to
dereference. Also, it depends on the circumstances of the caller, if
the caller does not have the values in registers it may be less
expensive computationally to pass the pointers instead of dereferencing
in the caller.

The common wisdom however is - the fewer instructions, the faster the
code. The correct thing to do is to though is benchmark your code. In
very few circumstances will this really make a difference.
 
G

gabriel

Gianni said:
Disassembly of section .text: [...]

Don't forget, though, that there are many variables here, such as
optimizing compilers, (as you mention) inlines, crappy compilers, and
hardware differences, none of which can be controlled by the source code
alone. For example, the compiler may dereference the pointer only once
at the beginning of the routine, use a temp value through the routine,
and then dereference the pointer again at the end to put the value back.
Though doing this would wreak havoc on multi threaded systems that access
the same pointers simultaneously, I can definitely see this being done by
a compiler coder tryin' to be fast...

The common wisdom however is - the fewer instructions, the faster the
code. The correct thing to do is to though is benchmark your code.

I would say that this is yesterday's common wisdom. Optimizers throw a
wrench into this nowadays, so very compact and obfuscated code does not
necessarily guarantee fast or efficient execution.

Unless you use an old compiler and you are very familiar with what
assembler code is generated by which source code instructions, you are
better off speding time optimizing your _algorithms_ and letting the
modern compiler figure out which assembler code to generate.
In very few circumstances will this really make a difference.

Very, very true. Time is better spent somewhere else the vast majority
of the time.
 
A

Andrew Koenig

Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,
etc.)

void scale(vector<double>& v, double& factor)
{
for (vector<double>::iterator it = v.begin(); it != v.end(); ++it)
*it *= factor;
}

If you have a vector<double> named, say, x, and you call scale(x, x[0]), the
roof caves in.

If factor is double rather than double&, all is well.
 
J

jeffc

jeffc said:
Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,
etc.)

Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.
 
T

Thomas Matthews

jeffc said:
etc.)

Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.

What people are saying is that your answers are compiler and
platform dependent. One compiler could use registers to pass
by value and reference, thus making them both equal.

The primary difference is mutable. Passing by value is passing
a copy of parameter. Passing by reference allows the original
variable to be modified. If one is to compare apples to apples
the one should compare a constant reference to pass by value.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

jeffc

Thomas Matthews said:
What people are saying is that your answers are compiler and
platform dependent. One compiler could use registers to pass
by value and reference, thus making them both equal.

The primary difference is mutable. Passing by value is passing
a copy of parameter. Passing by reference allows the original
variable to be modified. If one is to compare apples to apples
the one should compare a constant reference to pass by value.

Thomas, I understand. I was hoping someone might know off the top of their
heads where I could find something like your description, or anyone's
description, preexisting on a web site. In other words, if you know of a
web site (could be a FAQ, or any other instructional or informational web
site) that says it is compiler or platform dependent, or that the primary
difference is mutable, or that it makes little difference in practice, or
whatever. I was not able to find a site with such info in a search. Again,
the answers have been helpful, but I'm not looking for the answers. I'm
just looking for web sites that mention the topic. thanks for your reply.
 
G

gabriel

jeffc said:
Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.

Jeez, do we gotta even do a google search for you? Just search through
groups.google.com for this thread and there is your web reference...
 
J

jeffc

gabriel said:
Jeez, do we gotta even do a google search for you? Just search through
groups.google.com for this thread and there is your web reference...

Obviously that would defeat the purpose of a *preexisting* reference. Look,
obviously I was unclear clear about the purpose - let's just drop it.
Thanks for the replies.
 
J

Jeff Schwab

gabriel said:
I would say that this is yesterday's common wisdom. Optimizers throw a
wrench into this nowadays, so very compact and obfuscated code does not
necessarily guarantee fast or efficient execution.

I believe Gianni meant machine-language instructions, not lines of C++ code.

There's a great section in the Josuttis templates book on defining
functions that take arguments by value or reference, depending on a
number of factors.
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top