Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method with many parameters - Performance [closed]

Is it unefficient in terms of computing performance when i use have/use methods with alot (more than hundred) parameters ?

I don't mean efficient in terms of maintainability, but only in "raw" computing performance :)

like image 729
Nick Russler Avatar asked Dec 07 '12 18:12

Nick Russler


3 Answers

Theoretically, perhaps, since Java is pass-by-value, meaning that when a function is called, the JVM makes a copy of every parameter value and gives the copies to the function, so there may be some point at which the number of parameters has a non-negligible effect on execution time. But in practice, whenever possible, these copies are "shallow" copies, meaning that they're more like references, so there is very little time spent actually making the copies. So you would probably need a lot more than 100 parameters to have any noticeable impact on performance time.

In any case, even considering the performance time of something like this sounds very much like premature optimization. It is almost certainly not the bottleneck for your program, so it's not worth spending time on until you're certain that it's actually causing slowdown. If your program is unacceptably slow, investigate other possible sources of slowdown.

There is also, of course, as you mention, the issue of "maintainability." Why do you need hundreds of parameters for a single function? Are they complex parameters, such as ArrayLists of custom objects, or are they simple built-in data types? If the latter, why not consider packing them together into arrays, ArrayLists, and so on? Alternatively, why not break the function down into multiple functions? Modern computers are fast enough that for many (arguably most) purposes, programmer time is more valuable than processor time, so your first concern, when coding, should usually be whether what you're writing is understandable and well-written, not whether it's fast.

like image 119
Kyle Strand Avatar answered Sep 28 '22 00:09

Kyle Strand


As the article below says, it would perform better with less parameters, but it's not a big performance hit. But you should consider passing an object.

http://www.dailyfreecode.com/forum/parameter-performance-21574.aspx

like image 33
Peter Rasmussen Avatar answered Sep 27 '22 23:09

Peter Rasmussen


Well there will be a cost. All those parameters will have to be pushed onto the stack. I don't imagine it would cause trouble in terms of computing power unless it was in a heavy loop though.

Are you mad at a fellow programmer? Or making some kind of programatically created code?

like image 44
vextorspace Avatar answered Sep 27 '22 22:09

vextorspace