Do I pass an int by value or reference in constructor?
Question posted by: amphetaman@gmail.com
(Guest)
on
July 4th, 2008 02:55 AM
I have a very simple class for storing two-dimensional coordinates:
class Point
{
public:
Point();
Point(const Point &);
Point &operator(const Point &);
int x;
int y;
};
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
|
|
July 4th, 2008 02:55 AM
# 2
|
Re: Do I pass an int by value or reference in constructor?
Point &operator(const Point &);
Oops, sorry, I missed a character there. It should be Point &operator
=(const Point &).
|
|
July 4th, 2008 03:05 AM
# 3
|
Re: Do I pass an int by value or reference in constructor?
On Jul 3, 10:47*pm, ampheta...@gmail.com wrote:
Quote:
I have a very simple class for storing two-dimensional coordinates:
>
class Point
{
public:
* * Point();
* * Point(const Point &);
* * Point &operator(const Point &);
* * int x;
* * int y;
>
};
>
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
|
For built-in types, you shouldn't see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.
Joe Cook
|
|
July 4th, 2008 03:05 AM
# 4
|
Re: Do I pass an int by value or reference in constructor?
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
Quote:
For built-in types, you shouldn't see any performance difference
passing by reference or value. In fact, for a class as simple as the
one you sent, the function call itself should be optimized away by the
compiler entirely.
>
Joe Cook
|
Thanks. But still, are there any guidelines on whether to pass by
value or by reference? Is one or the other a "better style"?
|
|
July 4th, 2008 03:05 AM
# 5
|
Re: Do I pass an int by value or reference in constructor?
ampheta...@gmail.com wrote:
Quote:
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
|
Answer this 2 questions:
if passing a reference... what object/native type is pushed into the
stack?
if passing an int b value what object/native type is pushed into the
stack?
To answer the first, you should answer before yourself
What is a reference? (implementation point of view)
|
|
July 4th, 2008 04:05 AM
# 6
|
Re: Do I pass an int by value or reference in constructor?
<amphetaman@gmail.comwrote in message
news:b8d4db54-00d2-44eb-9caa-c50d27182774@u6g2000prc.googlegroups.com...
Quote:
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
Quote:
>For built-in types, you shouldn't see any performance difference
>passing by reference or value. In fact, for a class as simple as the
>one you sent, the function call itself should be optimized away by the
>compiler entirely.
>>
>Joe Cook
|
>
Thanks. But still, are there any guidelines on whether to pass by
value or by reference? Is one or the other a "better style"?
|
I would say in general it is probably better to pass by const referance when
you can. In general meaing for structures, classes, etc.. since this would
be faster than passing by value. For built in types it is my understanding
that references may actually be a little slower (but not by much) and may
get optimized away anyway.
My general rules for paramters are:
Make const whever I can
Make it a reference whenever I can and it's not built in
Don't make it a reference if it obfuscates the code.
Your mileage may vary.
--
Jim Langston
|
|
July 4th, 2008 08:45 AM
# 7
|
Re: Do I pass an int by value or reference in constructor?
On Jul 4, 10:47*am, ampheta...@gmail.com wrote:
Quote:
I have a very simple class for storing two-dimensional coordinates:
>
class Point
{
public:
* * Point();
* * Point(const Point &);
* * Point &operator(const Point &);
* * int x;
* * int y;
>
};
>
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
|
I prefer Point(int x, int y). Because it may do dereferencing if you
use Point(const int& x, const int& y). But as joseph said, compiler
could do optimizing for you. And Point(const int x, const int y) is
recommended if you want to express the constance of the parameters.
|
|
July 4th, 2008 09:45 AM
# 8
|
Re: Do I pass an int by value or reference in constructor?
On Jul 4, 5:00 am, ampheta...@gmail.com wrote:
Quote:
On Jul 3, 10:58 pm, joseph cook <joec...@gmail.comwrote:
|
Quote:
Quote:
For built-in types, you shouldn't see any performance
difference passing by reference or value. In fact, for a
class as simple as the one you sent, the function call
itself should be optimized away by the compiler entirely.
|
>
Thanks. But still, are there any guidelines on whether to pass
by value or by reference? Is one or the other a "better
style"?
|
It depends on who wrote the style guidelines:-). The most
frequent recommendation seems to be to use pass by value for
scalar types, pass by const reference for class types (and pass
by const reference in templates, when the actual type isn't
known). About the only other recommendation I've heard is to
consider pass by const reference an "optimziation", to be
applied only when the profiler says it's necessary.
Of course, a lot of people don't bother with guidelines, and do
things on an ad hoc basis, according to their feeling of the
moment. Thus, the standard library tends to pass iterators and
all types of functional objects by value, even though they're
not scalar types.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
|
|
July 4th, 2008 09:15 PM
# 9
|
Re: Do I pass an int by value or reference in constructor?
Join Bytes! kirjutas:
Quote:
I have a very simple class for storing two-dimensional coordinates:
>
class Point
{
public:
Point();
Point(const Point &);
Point &operator(const Point &);
int x;
int y;
};
>
If I want a constructor that takes two ints, is it better to pass by
value or by reference? In other words, Point(int, int) or Point(const
int &, const int &)? (Or maybe Point(const int, const int)? Does that
actually improve performance?)
|
It does not make any difference, unless your class is used in a tight
loop in a performance-critical application (and then you have to profile
anyway to find out the bottlenecks).
That said, on current hardware a reference typically takes 64 bit plus a
dereference step and an int typically takes 32 bit and no dereference. So
I would go with pass by value in this case, as it is most probably not
slower than the alternative. If the functions are inline, the slower
version is probably optimized to the other anyway, so it's actually not
so important point to worry about.
hth
Paavo
PS. The 'const int' version you mentioned is the same as 'int',
performance-wise. The const keyword in general cannot be used for
optimization because it could be cast away legally in the inners of the
function.
|
|
July 4th, 2008 09:55 PM
# 10
|
Re: Do I pass an int by value or reference in constructor?
"Alf P. Steinbach" <alfps@start.nokirjutas:
Quote:
Not all the world is 64-bit Windows.
|
I was primarily thinking of 64-bit Linux, on a 16-core Intel CPU. It's one
of our main development targets currently.
YMMV of course ;-)
Best regards
Paavo
|
|
July 5th, 2008 07:35 AM
# 11
|
Re: Do I pass an int by value or reference in constructor?
On Jul 4, 11:40 pm, "Alf P. Steinbach" <al...@start.nowrote:
[...]
Quote:
Quote:
That said, on current hardware a reference typically takes
64 bit plus a dereference step and an int typically takes 32
bit and no dereference.
|
|
Quote:
Not all the world is 64-bit Windows.
|
Nope. Most of us have been 64 bits for close to 10 years
now:-). (Except maybe for mainframes, where 36 and 48 bits can
also be found. Or for those working on embedded processors,
where I think 16 bits is still common. Or who knows what else.)
And yes, I'm just trying to be humorous. Your point is well
taken (although you can drop the Windows---my 64 bit machines
run under Solaris or Linux).
(Of course, all considerations with regards to what the compiler
might generate, supposing any specific architecture, are really
irrelevant anyway. Since compilers don't always do what we
expect. Any rule is arbitrary, so just pick one, and stick to
it until actual measurements say you need to change.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
Not the answer you were looking for? Post your question . . .
189,075 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|