Is using static private methods really faster/bett

2019-04-13 06:55发布

What I'm asking is whether there is a difference between doing this:

public Something importantBlMethod(SomethingElse arg) {
    if (convenienceCheckMethod(arg)) {
        // do important BL stuff
    }
}

private boolean convenienceCheckMethod(SomethingElse arg) {
    // validate something
}

And this:

public Something importantBlMethod(SomethingElse arg) {
    if (convenienceCheckMethod(arg)) {
        // do important BL stuff
    }
}

private static boolean convenienceCheckMethod(SomethingElse arg) {
    // validate something
}

I actually use option 1 as it seems more natural to me.

So is there a style/convention/performance difference between the first and the second way ?

Thanks,


As suggested in the comments I tested it, in my benchmarks the dynamic method is faster.

This is the test code:

public class Tests {

    private final static int ITERATIONS = 100000;

    public static void main(String[] args) {
        final long start = new Date().getTime();

        final Service service = new Service();
        for (int i = 0; i < ITERATIONS; i++) {

            service.doImportantBlStuff(new SomeDto());
        }

        final long end = new Date().getTime();

        System.out.println("diff: " + (end - start) + " millis");
    }
}

This is the service code:

public class Service {

    public void doImportantBlStuff(SomeDto dto) {

        if (checkStuffStatic(dto)) {

        }

        // if (checkStuff(dto)) {

        // }
    }

    private boolean checkStuff(SomeDto dto) {
        System.out.println("dynamic");
        return true;
    }

    private static boolean checkStuffStatic(SomeDto dto) {
        System.out.println("static");
        return true;
    }
}

For 100000 iterations the dynamic method passes for 577ms, the static 615ms.

This however is inconclusive for me since I don't know what and when the compiler decides to optimize.

This is what I'm trying to find out.

8条回答
姐就是有狂的资本
2楼-- · 2019-04-13 07:25

It might, it might not. It might be different between different executions of your code.

Here's the only thing that you can know without digging into the Hotsport code (or the code of your non-Hotspot JVM):

  • The static method is invoked with invokestatic, which does not require an object reference.
  • The instance private method is invoked with invokespecial, which does require an object reference.

Both of those opcodes have a process for resolving the actual method to invoke, and those processes are relatively similar (you can read the specs). Without counting the instructions of an actual implementation, it would be impossible to say which is faster.

The invokespecial pushes an extra value onto the stack. The time to do this is counted in fractions of a nanosecond.

And making all of this moot, Hotspot has a wide range of optimizations that it can perform. It probably doesn't have to do the actual method resolution more than once during your program's run. It might choose to inline the method (or it might not), but that cost would again be roughly equivalent.

查看更多
趁早两清
3楼-- · 2019-04-13 07:29

According to me NO binding of static method is same as non-static private i.e early binding. . Compiler actually adds code of method (static or non-static private) to your code while creating it's byte code.

Update : Just came through this article. It says instance methods binding is dynamic so if method is not non-static private then. Your static method is faster.

查看更多
该账号已被封号
4楼-- · 2019-04-13 07:32

Performance wise: The difference, if any, is negligible.

The rule of thumb is to declare your method static if it doesn't interact with any members of its class.

查看更多
对你真心纯属浪费
5楼-- · 2019-04-13 07:35

It all depends on the context. Generally static methods/variables are declared in a class so that an external class can make use of them.

If you are making a call to a local method then you should generally use instance methods rather than making static calls.

FYI, your syntax for calling the static method from an instance method is wrong. You have to supply the class name.

查看更多
混吃等死
6楼-- · 2019-04-13 07:43

If the result of the function does not depend on anything but the arguments, it should be static. If it depends on an instance, make it an instance member.

It's not about performance; it's about semantics. Unless you're calling this function a million times a second, you will not notice a performance difference, and even then the difference won't be significant.

查看更多
叼着烟拽天下
7楼-- · 2019-04-13 07:45

I take part in coding competitions and I have observed, that non-static methods are faster(however minimal) than the static methods. Of course, it depends on your use and the what the situation demands, but the static methods gives poorer performance as compared to non-static ones. By convention, you can use static methods for the ease of code, but creating an instance of the class and calling the method will give better performance.

查看更多
登录 后发表回答