简体   繁体   中英

Java Best Practices: Performance with method parameters

Which is faster and/or less resources consuming:

class Foo()
{
    public int value;
}

This way?

public int doSomeStuff(Foo f)
{
    return (f.value + 1);
}

public int doOtherStuff()
{
    ...
    Foo f = new Foo();
    int x = doSomeStuff(f);
    ...
)

or this way?

public int doSomeStuff(int v)
{
    return (v + 1);
}

public int doOtherStuff()
{
    ...
    Foo f = new Foo();
    int x = doSomeStuff(f.value);
    ...
)

In both cases, "doSomeStuff" will not change nothing in foo class. It just needs to know the "value".

They both perform the same, the same sequence of operations occurs. Your main concern is maintainability and sensible design here. Think carefully about which methods need which data and design it properly.

If you do have issues, you can optimise later. But you should always optimise last.

In terms of resource consuming, it is exactly the same. But the second option is clearly better in terms of programming because if doSomeStuff only needs value, then there is no point to passing f.

I don't think there is any performance difference at all. And Java compiler will optimize to the best one anyway...

取决于您在不调用doSomeStuff情况下调用doOtherStuff ,但通常性能差异可以忽略不计,如果您只调用doOtherStuff那么它们的性能相同。

Probably even better:

Decalre doSomeStuff() as a method of foo , and invoke: f.doSomeStuff()

It is much more readable and will be easier to maintain doing it so, since if you have a sub class of foo : Bar , and you want to calculate things a bit different - all you have to do is override doSomeStuff() in Bar

You should prefer readability over micro optimizations - let the compiler take care of those for you.

code snap:

class foo() {
    public int value; 
    public int doSomeStuff() {
       return value + 1;
    }
}

and:

public int doOtherStuff() {
    ...
    foo f = new foo();
    int x = f.doSomeStuff();
    ...
}

The difference between doing:

object.intvariable + 1

and

int + 1

is so negligible as to be irrelevant for real world apps. It's probably one or two more JVM opcodes to look up foo and find its value variable which is not worth mentioning. You'd never notice that unless you were trying to create a pseudo real-time app in Java (which is all but an exercise in futility).

However, that said, the way you are doing it is very bad. You should not be exposing value directly, but be using proper data encapsulation via getter and setter methods.

It does not matter from performance perspective. The recommendation is: do not think about pre-mature optimization. Think about correctness and good design of your code.

For example your code

  1. Does not follow naming conventions: class names must start with capital letter
  2. Contains public fields. It is forbidden. Use bean notation (getters and setters).
  3. Cannot be compiled (there is no type integer . Choose among int and Integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM