Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can passing an object to a method increase performance over sending individual parameters

I am assigned to a job of optimizing performance of an Application.

Often it is needed to pass 16-25 parameters to the constructor, and setting them there. I want to create a class & object for this and set these values to the object and then pass. It will be good to read. But will it do any good to my task (i.e performance optimization)?

like image 887
Shashank Avatar asked Jul 13 '15 06:07

Shashank


People also ask

What happens when an object is passed to a method as a parameter?

When a parameter is pass-by-reference, the caller and the callee operate on the same object. It means that when a variable is pass-by-reference, the unique identifier of the object is sent to the method. Any changes to the parameter's instance members will result in that change being made to the original value.

What happens when you pass an object to a method in Java?

Java always passes parameter variables by value. Object variables in Java always point to the real object in the memory heap. A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value.

Can you pass an object to a method?

We can pass the data to the methods in form of arguments and an object is an instance of a class that is created dynamically. The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method.

Can we pass an object as parameter to call a method in Java?

In Java, we can pass a reference to an object (also called a "handle")as a parameter.


2 Answers

It might improve the performance slightly, but the main benefit would be increasing the readability of your code. A constructor with 16-25 parameters is not readable, and very hard to use.

Of course, you should only introduce new classes that make sense (i.e. the parameters are related to each other). There's no point is shoving 15-26 unrelated parameters to one class just for the sake of passing them to a constructor.

like image 171
Eran Avatar answered Sep 27 '22 19:09

Eran


Performance differences deriving from this, if any, are very unlikely to be noticeable.

If your task is to solve performance problems, then your first job is to locate where the problems are. You do that by profiling the application. Have you done that and has it demonstrated that the problem is in the parameter passing ?

like image 30
Erwin Smout Avatar answered Sep 27 '22 19:09

Erwin Smout