So my question is, what are the real advantages/dissadvantages of the
following (style comments are welcome as well): Passing multiple
parameters, passing a structure, or passing a pointer to a structure?
John
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Definition of efficiency?
>This
>seems a bit counter intuitive to me, as every time you access a member
>of
>this structure pointer, you need to resolve a pointer, which I would
>imagine
>would be more expensive than putting the data on the stack.
Quite possibly not.
>
>So my question is, what are the real advantages/dissadvantages of the
>following (style comments are welcome as well): Passing multiple
>parameters,
Means that the programmer has to remember the order in which the
arguments must be provided. It also means that any possible default
values can only by provided for parameters at the tail end (well
overloading can help with this problem)
Anytime you have more than half a dozen parameters you have a memory
burden for the user that suggests investigating alternatives.
> passing a structure,
Requires copying, which with that number of parameters may well not be
done into registers. So you pay for accessing a struct and for copying
it.
> or passing a pointer to a structure?
(or reference)
Now you can reset the arguments you want to change (in C++ you can even
provide a ctor that fills in default values for all arguments). You pay
for dereferencing a pointer but the cost will vary from zero to
insignificant depending on the quality of optimisation and the
facilities of the underlying hardware.
In fact I can imagine circumstances where this mechanism could result in
measurable gains as the whole of the struct could be held in cache where
access would be very fast.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
At the machine code level, the differences will probably be quite
minor. Whether the stuff is in a structure or as separate parameters
on the stack, you're still storing and retrieving values at various
offsets from a base pointer (pointer to the structure or pointer to
the stack frame). At worst, this would use up an extra machine
register in the called function. You also have the extra overhead of
storing a pointer to the structure on the stack, which you wouldn't
need with raw parameters. If you are really optimizing the code to
that kind of level (why?), the only way to know how much difference it
makes is to measure it. You'll probably find that there is no
measurable difference.
>
> So my question is, what are the real advantages/dissadvantages of
the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a
structure?
If you're passing a struct parameter, reference to const is probably
the best default choice. I guess a struct does have an advantage in
terms of clarity compared to multiple parameters, since the code can
show meaningful names for the sub-parts:
info s;
s.some_name = 3;
s.extra_something = "xyz";
s.more_stuff = 8.0;
f (s);
instead of f(3, "xyz", 8.0);
On the other hand, twelve parameters is quite a lot for one function,
however you cut it. Maybe you can break them into functional groups
and wrap each group in a class with a meaningful name:
f (machine_type("bogo", 8000)
, os_version ("bogos", 3.2)
, keyboard_type ("uk", 102));
Or some such (trying to imagine what the twelve parameters could be).
Ideally, I suppose the twelve-parameter function would itself become a
member function of some suitable class, and get most of its inputs as
member variables.
Regards,
Raoul Gough.
At the machine code level, the differences will probably be quite
minor. Whether the stuff is in a structure or as separate parameters
on the stack, you're still storing and retrieving values at various
offsets from a base pointer (pointer to the structure or pointer to
the stack frame). At worst, this would use up an extra machine
register in the called function. You also have the extra overhead of
storing a pointer to the structure on the stack, which you wouldn't
need with raw parameters. If you are really optimizing the code to
that kind of level (why?), the only way to know how much difference it
makes is to measure it. You'll probably find that there is no
measurable difference.
>
> So my question is, what are the real advantages/dissadvantages of
the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a
structure?
If you're passing a struct parameter, reference to const is probably
the best default choice. I guess a struct does have an advantage in
terms of clarity compared to multiple parameters, since the code can
show meaningful names for the sub-parts:
info s;
s.some_name = 3;
s.extra_something = "xyz";
s.more_stuff = 8.0;
f (s);
instead of f(3, "xyz", 8.0);
On the other hand, twelve parameters is quite a lot for one function,
however you cut it. Maybe you can break them into functional groups
and wrap each group in a class with a meaningful name:
f (machine_type("bogo", 8000)
, os_version ("bogos", 3.2)
, keyboard_type ("uk", 102));
Or some such (trying to imagine what the twelve parameters could be).
Ideally, I suppose the twelve-parameter function would itself become a
member function of some suitable class, and get most of its inputs as
member variables. It really depends how many places need to use the
function as to whether it's worth going to the trouble.
Regards,
Raoul Gough.
I agree. Instead of copying 12 parameters on the stack, only a reference
is copied (4 bytes on most systems).
> This
> seems a bit counter intuitive to me, as every time you access a member
> of
> this structure pointer, you need to resolve a pointer, which I would
> imagine
> would be more expensive than putting the data on the stack.
I'm not convinced that accessing a structure member is more expensive.
>
> So my question is, what are the real advantages/dissadvantages of the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a structure?
>
>
When dealing with a lot of parameters, I think is better to pass the
parameters as a structure (a reference to the structure, not pointer):
void f( const MyStructure& params) { ... }
Of course, there can be some additional code to write: initializing the
structure.
I think that the decision of wether using a structure or parameters
depend on the context the function will be used: the number of calls vs.
the number of structure initialisations.
In a context like the following (multiple calls using the same
structure):
MyStructure s;
// initialisation of s
f( s);
// some code
f( s);
it is better to use the structure.
But, if you need to initialize the structure before every function call,
passing the structure as a parameter doesn't present an advantage.
Catalin Pitis