Is using static private methods really faster/better than instance private methods?

左心房为你撑大大i 提交于 2019-12-04 17:34: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.

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.

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.

If your method requires instance data or calls to other instance methods, it must be an instance method.

If the function only depends on its arguments, and no other static data, then it might as well be an instance method too - you'll avoid the need to repeat the class name when you invoke the static function.

IMHO, there's no particular need to make the function static unless:

  1. it's callable from other classes (i.e. not private), and
  2. it doesn't refer to instance variables, and
  3. it refers to other static class data

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.

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.

I checked, I hope it does what you wanted to know, the code won't be beautiful:

public class main {

@SuppressWarnings("all")
public static void main(String[] args) {
    main ma = new main();
    int count = Integer.MAX_VALUE;
    long beg = (new Date()).getTime();
    for (int i = 0; i < count; i++) {
        ma.doNothing();
    }
    System.out.println("priv : " + new Long((new Date()).getTime() - beg).toString());

    beg = (new Date()).getTime();
    for (int i = 0; i < count; i++) {
        doNothingStatic();
    }
    System.out.println("privstat : " + new Long((new Date()).getTime() - beg).toString());

}

private void doNothing() {
    int i = 0;
}

private static void doNothingStatic() {
    int i = 0;
}
}

results:

priv : 1774
privstat : 1736

priv : 1906
privstat : 1783

priv : 1963
privstat : 1751

priv : 1782
privstat : 1929

priv : 1876
privstat : 1867

It doesn't look like dependent on static - nonstatic private method. I am sure the differences are coming from the current burden of the machine.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!