Archived

This topic is now archived and is closed to further replies.

n0ob

How many parameters is too many?

Recommended Posts

Well, rather, how much parameter data is too much to pass into a function for performance concerns?

Share this post


Link to post
Share on other sites
Im guessing it would really depend on what the parameters were.

If you''re passing a large amount of structs or something, I could see it slowing you down, unless maybe if you''re using references

Share this post


Link to post
Share on other sites
It is sensible to pass as little data as you possibly can. The exact number of parameters is not really an issue, more the total amount of data that has to be sent. If one uses a reference as a parameter, it can be optimised by the compiler to require no data, or a pointer. A Pointer to a data structure of arbitrary size takes just 32-bits (4 bytes) on a 32-bit system. So basically, for max performace, use only references if possible, or pointers if necessary. Only ever pass by value if you require a local copy of the information to be made (ie use pass by value instead of using a reference and putting it in a local variable). On the topic of parameters, unless you specifically require it, make variables const type* of const type&. This will make sure you don''t accidentally change the parameter objects unintentionally.

Share this post


Link to post
Share on other sites
a normal recommendation is to pass by value for built-in types (like int, float, bool, etc) and by reference for all other types.

use const & or const * whenever possible, i.e. when you''re not going to change the data. Not only does the compiler enforce that but that little code also tells other coders that their value won''t be changed when passed to your function.

I don''t think the actual number of parameters is a real problem (heh, just check Windows API or DX for some funky stuff) but why you''re passing so much stuff.

}-- Programmer/Gamer/Dreamer --{

Share this post


Link to post
Share on other sites
quote:
Original post by dmounty
So basically, for max performace, use only references if possible, or pointers if necessary.

It really depends what you''re passing. Most processors are optimised to deal with 32-bit data so you might find it more efficient than smaller parameters anyway. But you should definatly use pointers instead of sending structures / classes as parameters.

Share this post


Link to post
Share on other sites
Id also say too many parameters would be when it takes a lot of effort just to remember each parameter

Share this post


Link to post
Share on other sites
I start cringing when its more than three.

But when you''re arguing "for performance reasons" I''ll just say this:
Rant: PREMATURE OPTIMIZATION SUCKS
More tact rant:
The place you will be hurting for speed, you won''t be passing like 12 variables by value anyway. At least I hope not.

Share this post


Link to post
Share on other sites
I agree with Antareus that premature optimisation is a bad thing, mmm''kay? On the other hand, it''s also a good thing to know your compiler and have some understanding of how it generates code, and avoid things you know will give you a performance hit.

The ARM C compiler I use will pass up to four parameters via registers (r0-r3). Add any more parameters than that, and it starts pushing them onto the stack, which is much slower in comparison. I break into a cold sweat if I find myself adding a 5th parameter...

Share this post


Link to post
Share on other sites

Hitchhiker's Guide To The Galaxy




The answer to life, the universe, and everything is...


42



[edited by - gh_0_st on October 13, 2003 9:36:35 AM]

Share this post


Link to post
Share on other sites
Yeah, don''t worry about performance; it''s more of a style issue. Remember; the time that you will spend writing 12 parameters every time you call the function could be spent optimizing a more critical piece of code.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
" Well, rather, how much parameter data is too much to"
" pass into a function for performance concerns? "

There are 3 considerations you need to look at: 1) how many times this function will be called, 2) how the parameters are used internally, and 3) how the data is organized externally.

1) Functions called in a tight loop need as little parameters as possible. Some other functions might be generalizations (like SQL calls) or open-ended such as printf(...) for which it is expected to have a long list of parameters. But generally, the less parameters you pass in the better.

2) Some of those parameters will be used as constants other will be used dynamically during the execution of the function. For example, you might have an int that specifies which algo to use or some mode your function needs to take into account while a floating point parameter will be modified during execution. Things that have the tendency to stay constant over a large number of uses should be put in a struct and passed by pointer. Also, if the function calls other functions in cascade, then putting parameters in a struct you pass around by pointer is a better solution than having to expand them canonically in formal formal parameters for each sub-function.

3) If the data you pass in to a function comes from structured storage areas, then you should pass pointer to those instead. Unless of course you can''t make any assumption about the app''s data organization because you''re building a library.

Hope this helps.

Share this post


Link to post
Share on other sites
If you see a group of functions in which you often pass the same arguments, it''s an indication that you could probably turn them into a class that has these parameters as member variables (of course, don''t do it unless these functions show to belong together logically)

Share this post


Link to post
Share on other sites