I have heard it is considered good practice to pass function parameters as
const& as often as possible, is this true? Is it possible to go overboard?
And if so why? Thanks a lot in advance everyone!
Christopher Diggins http://www.cdiggins.com 20 2130
christopher diggins wrote: I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance everyone!
Certainly...it makes little sense to pass things that aren't very big
to begin with (pointers, ints, doubles, etc...) via const reference when
a copy would most likely be faster.
However when it comes to things where the copying is nontrivial: vectors,
strings, etc...and you know that it's not going to change in the caller,
then there's an advantage to not copying it.
"christophe r diggins" <cd******@video tron.ca> wrote in message news:3k******** *************@w eber.videotron. net... I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard?
I have been known to say "The only rule of thumb worth a damn is
'Do not rely on rules of thumb without knowing when to break them.'".
The rule you suggest is not worth a damn, period. So applying it
would be quite likely to lead to overuse.
And if so why? Thanks a lot in advance everyone!
For objects that are expensive to copy, which includes large objects
or objects whose copy contructors must allocate dynamic memory,
passing via const reference would be good idea, unless there were
locking issues that would make even the copying a cheaper choice.
When considering the gain achieved by avoiding a copy, one must
consider the extra indirection that a reference will usually entail for
each access to the members or value of the object.
The easiest way to realize that the rule is bunk is to consider the
case of simple value objects that (may, on a particular machine)
fit in a machine word, such as int, float, or small structs. When
these can be copied to the parameter/call stack with a single
instruction, using a reference instead would likely be a loser.
But even that cannot be taken as a hard and fast rule. For
example, such a small object might be only rarely used in the
called routine and tend to reside in memory that has been
paged out so that passing its address and not using it would
be faster than pulling the value at the cost of a page fault.
Good practise is to understand what affects performance and
keep it in mind during design and coding, but finally measure
performance and, if lacking, discover why and correct. If
silly errors have not been made in the design, such fixes as
are needed at that point should be mostly localized changes.
And, in case I have not been clear about this: Good practise
is to avoid rules of thumb without knowing their limitations.
Christopher Diggins http://www.cdiggins.com
--
--Larry Brasfield
email: do************* **********@hotm ail.com
Above views may belong only to me.
christopher diggins wrote: I have heard it is considered good practice to pass function parameters as const& as often as possible. Is this true?
Yes.
You can also pass small objects by value.
Is it possible to go overboard? And, if so, why?
You should pass by value, const* or const& and return by value.
Functions that return void are seldom justified.
A destructor is an example of an exception to this rule.
Sometimes, it is necessary to modify a large object *in-place*.
Examples are:
discrete finite Fourier transforms, matrix decompositions and
other operations on container objects.
Assignment:
X& X:operator=(con st X&);
is an example of a function that modifies one of its arguments --
the hidden argument X* this.
Functions that modify one of their arguments
should return a reference or a pointer to the modified object
instead of void so that the function can be used in an expression.
But, generally, if you find yourself implementing a function
that modifies one or more of its arguments,
you should stop and think carefully about your design.
The argument that is modified must be declared as a [state] variable
in the calling program and this will complicate
the analysis of your program by you and other programmers
who must maintain your program.
"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com... christopher diggins wrote: I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance everyone!
Certainly...it makes little sense to pass things that aren't very big to begin with (pointers, ints, doubles, etc...) via const reference when a copy would most likely be faster.
However when it comes to things where the copying is nontrivial: vectors, strings, etc...and you know that it's not going to change in the caller, then there's an advantage to not copying it.
Thanks for the response. Do you think excessive copying of primitive types
lead to a significantly performance penalty given the optimizations
performed by most C++ compilers? Is there any reason an optimizer would not
make a copy of a superflous reference to a primitive type?
Christopher Diggins
>>I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? I have been known to say "The only rule of thumb worth a damn is 'Do not rely on rules of thumb without knowing when to break them.'".
The rule you suggest is not worth a damn, period. So applying it would be quite likely to lead to overuse.
And if so why? Thanks a lot in advance everyone!
For objects that are expensive to copy, which includes large objects or objects whose copy contructors must allocate dynamic memory, passing via const reference would be good idea, unless there were locking issues that would make even the copying a cheaper choice. When considering the gain achieved by avoiding a copy, one must consider the extra indirection that a reference will usually entail for each access to the members or value of the object.
The easiest way to realize that the rule is bunk is to consider the case of simple value objects that (may, on a particular machine) fit in a machine word, such as int, float, or small structs. When these can be copied to the parameter/call stack with a single instruction, using a reference instead would likely be a loser. But even that cannot be taken as a hard and fast rule. For example, such a small object might be only rarely used in the called routine and tend to reside in memory that has been paged out so that passing its address and not using it would be faster than pulling the value at the cost of a page fault.
Could modern C++ optimizing compilers compensate for superflous usage of
const& with small value types?
Good practise is to understand what affects performance and keep it in mind during design and coding, but finally measure performance and, if lacking, discover why and correct. If silly errors have not been made in the design, such fixes as are needed at that point should be mostly localized changes.
And, in case I have not been clear about this: Good practise is to avoid rules of thumb without knowing their limitations.
You are preaching to the choir here, I am asking in order to have a better
understanding of how superflous const& operations on small value types could
adversely affect the performance. To this end I significantly appreciate
your input.
Christopher Diggins
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cm******** **@nntp1.jpl.na sa.gov... christopher diggins wrote:
I have heard it is considered good practice to pass function parameters as const& as often as possible. Is this true?
Yes. You can also pass small objects by value.
Is it possible to go overboard? And, if so, why?
You should pass by value, const* or const& and return by value. Functions that return void are seldom justified. A destructor is an example of an exception to this rule. ...
Thanks for the response, but I think we have crossed wires. I am pondering
the negative effects that would arise if I implemented a large library with
every single argument being passed as a const&. Clearly there is some
measure of performance penalty, but I am trying to gauge how much.
Christopher Diggins
christopher diggins wrote: Could modern C++ optimizing compilers compensate for superflous usage of const& with small value types?
Yes.
But it isn't practical.
You can't measure the difference between
pass by value and pass by const reference
for small objects in any function call.
Try it for yourself.
Pass built-in types by value
and pass User Defined Types (UDTs) by const reference
and your code won't be far from optimal wherever you port it.
christopher diggins wrote: Thanks for the response, but I think we have crossed wires. I am pondering the negative effects that would arise if I implemented a large library with every single argument being passed as a const&. Clearly there is some measure of performance penalty, but I am trying to gauge how much.
The performance penalty is so slight that
you won't be able to measure it.
> Pass built-in types by value and pass User Defined Types (UDTs) by const reference and your code won't be far from optimal wherever you port it.
Again, you can not make general statements like that without knowing the
implementation details. Consider a device controller programmer who has the
following UDT:
struct DISK_REGISTER {
unsigned ready:1;
unsigned error_occured:1 ;
unsigned disk_spinning:1 ;
unsigned write_protect:1 ;
unsigned head_loaded:1;
unsigned error_code:8;
unsigned track:9;
unsigned sector:5;
unsigned command:5;
};
For sake of argument, assume: sizeof(DISK_REG ISTER) == sizeof(int).
Now, say we needed to pass this struct into a function and modify all its
members. The extra level of indirection required by a const reference to
access the members would be much more inefficient than simply passing in the
struct by value. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Victor Bazarov |
last post by:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is
AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing 'retval'
return retval;
}
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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,...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |