Jump to content
  • Advertisement

Archived

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

n0ob

How many parameters is too many?

This topic is 5367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!