473,626 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.)
Jul 22 '05 #1
12 2117
jeffc wrote:
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...).

--
gabriel
Jul 22 '05 #2
jeffc wrote:
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_referen ce( 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_re ferenceRiS_>:
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_va lueii>:
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_ref erenceRiS_:
ld [%o0], %g1
ld [%o1], %o2
retl
add %g1, %o2, %o0
_Z13pass_by_val ueii:
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.

Jul 22 '05 #3
Gianni Mariani wrote:
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.

--
gabriel
Jul 22 '05 #4
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
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<do uble>& 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.
Jul 22 '05 #5

"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
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.
Jul 22 '05 #6
jeffc wrote:
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
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.


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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #7

"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:Ci******** **********@news svr16.news.prod igy.com...

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, 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.
Jul 22 '05 #8
jeffc wrote:
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.c om for this thread and there is your web reference...

--
gabriel
Jul 22 '05 #9

"gabriel" <no@no--spam.com> wrote in message
news:be******** *************** ****@msgid.mega newsservers.com ...
jeffc wrote:
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.c om 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.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2952
by: Philip D Heady | last post by:
Anyone know where i can find all world currencies (USD,etc) and do an import into mySQL? Just need a comma delimited list if possible. Example: USD, United States Dollar etc, etc
4
6035
by: marco_segurini | last post by:
Hi, I am wondering if there is a way to use the ternary operator to select a function, between two, even if at least one of them is overloaded. Ex: void f1(int) {} void f1(double) {} void f2(int) {} //void f2(double) {}
0
411
by: Engin H. | last post by:
Hi all I have a ASP.NET web project and i develop a Windows Control Library, which references Excel interop, in my web project. In my computer it works ok, when i deploy the project to the server, the clients cannot fint Excel interop and due to cannot load Control Library. How can i reference Excel interop on server side? Engin H.
13
2167
by: Elaine | last post by:
This has to do with self-joins and includes a running balance problem. Is it possible to do this using SQL alone? Amortization schedule -------------------------- Givens: beginning balance, payment amount, # of periods, rate per period schedule would consist of columns for balance, principal paid,
5
1676
by: Zanzibar | last post by:
H Is it possible that the Personal edition of C#, provided for free with the book Study C# Deluxe, produces very slow code? When I write a loop at home that creates a 100,000 objects, it takes the App almost 1.5 minute. When I do this in VS 2002 enterprise, it takes only a couple of MSecs I was really disappointent about the speed yesterday, but I think I have a wrong version Kind regards Alexander
9
1693
by: Klaas Vantournhout | last post by:
Hi all, I have a question about friends functions of a template class. To make it simple, I would like to do something like this. Assume that I have a class foo with template T template<Tclass foo
11
2772
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that object is removed from the map. Since Boost's shared_ptr class allows a custom deleter to be specified, I was thinking about something like this:
4
2253
by: Doug Gray | last post by:
Folks, I am looking for a fast but most importantly a bullet proof method to pass and NMEA data stream (GPS output) ascii numeric strings. The best I can offer is: def fint(a): try: return int(float(a)) except: return 0 The reason for this is the quality of the data from the huge variety of
206
13216
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
1
1380
by: yuan | last post by:
halo.. i face problem when i execute the programming. it appear the error as : 1.obj : error LNK2001: unresolved external symbol "double __cdecl NR::trapzd(double (__cdecl*)(double),double,double,int)" (?trapzd@NR@@YANP6ANN@ZNNH@Z) Debug/1.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. can anyone help me ? thanks in advance . below is the programming :
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.