Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

parameter performance

  Asked By: Madeline    Date: Sep 18    Category: Java    Views: 2607

I was wondering how passing parameters effects processor performance.


if I want a method to use variables a b and c

which are all stored in a class which stores variables a b c d e f and g

is it better to

pass them individually to the method like =

a =myClass.getA();
b =myClass.getB();
c =myClass.getC();
answer =someClass.someMethod(a,b,c);

or would it be better to just go write the method to accept the whole class




2 Answers Found

Answer #1    Answered By: Louis Mason     Answered On: Sep 18

I am not sure how Java does it, but I am quite sure on
how C/C++ does it, and I think it will be similar in
the JVM code. By passing  three paramaters, you have
to push the pointers to three values in the stack.
And one parameter  would only be pushing one value on
the stack, so therefore the less parameters  should be
faster, but this happens so quick it is not a
performance hit.

In your scenerio, I would pass the class's reference,
because the three get calls would be unnecessary. But
this does not mean everytime there are three or more
parameters create a class  to encapsulate the
parameters just to make it faster. This is definitely
not where you need to concentrate on performance

Answer #2    Answered By: Hehet Chalthoum     Answered On: Sep 18

I asked a mate too and he says that with java, because of the whole objects are
all passed by reference deal... its probably better to pass a whole class  in as
a paramater than getting individual copies of certain values.

Didn't find what you were looking for? Find more on parameter performance Or get search suggestion and latest updates.