Android java performance: invoking static method from self class or outer class

最后都变了- 提交于 2020-01-07 05:28:12

问题


by an high-performance point of view, calling an outer class static method is slower than calling a static method in the same class?


回答1:


No, never. Leaving method visibility apart, static method are just global function in the C sense. So it can't matter, what class they belong to, not even on Android.




回答2:


It depends. Consider the following:

public class Outer {
    /*private*/ static void outerMethod() {
        // do stuff
    }

    static class Inner {
        public void doStuff() {
            outerMethod();
        }
    }
}

The doStuff() method's bytecode looks like this:

     0: invokestatic  #2                  // Method Outer.outerMethod:()V
     3: return        

But if we make outerMethod() private, the code will look like this instead:

     0: invokestatic  #2                  // Method Outer.access$000:()V
     3: return        

The problem is that the inner class can't directly call the outer class method, because it's private. The compiler works around this by creating a synthetic access$000() method, which does nothing but call outerMethod(). Calling through the accessor method is slower unless the AOT or JIT compiler recognizes the pattern and optimizes it out.

So, calling an outer-class static method could be slower if it's also declared private.



来源:https://stackoverflow.com/questions/24210531/android-java-performance-invoking-static-method-from-self-class-or-outer-class

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