简体   繁体   English

Java静态/实例方法优化

[英]Java Static/Instance method optimization

In Java are unmodified method variables, that lack the final , qualifier re-initialized each time in a 在Java中未修改方法变量,缺少了final ,预选赛重新初始化在每次

  1. static method 静态方法
  2. instance method 实例方法

If the answer to 1. or 2., (or both) would the final qualifier allow Java to perform an optimization and store the method variable only once? 如果对1.或2。,(或两者)的答案是, final限定符将允许Java执行优化并仅将方法变量存储一次?

If the answer depends on the type of the variable, which type of variables are optimized/unoptimized? 如果答案取决于变量的类型,则优化/未优化哪种类型的变量? For instance, is String , int optimized while Map not optimized? 例如, Stringint优化而Map未优化?

For comparison, Java would only store a static class variable such as 为了进行比较,Java将仅存储静态类变量,例如

private static final String foo = "Teenage Mutant Ninja Turtle";

once. 一旦。 To clarify: the question is whether or not 澄清一下:问题是

1: 1:

static SomeReturnValueOrVoid SomeMethod() {
    // 1.a Not modified, is this reinitialized each method call?
    String foo = "Teenage Mutant Ninja Turtle";

    // 1.b Marked final, is this reinitialized each method call?
    final String bar = "Teenage Mutant Hero Turtle"; 
}

2: 2:

SomeReturnValueOrVoid SomeMethod() { // not static
    // 2.a Not modified, is this reinitialized each method call?
    String foo = "Teenage Mutant Ninja Turtle";

    // 2.b Marked final, is this reinitialized each method call?
    final String bar = "Teenage Mutant Hero Turtle"; 
}

is equivalent to 相当于

3: 3:

class SomeClass {
    static final String foo = "Teenage Mutant Ninja Turtle";

    SomeReturnValueOrVoid SomeMethod() {
        // Uses foo
    }

    static SomeReturnValueOrVoid SomeMethod() {
        // Uses foo
    }

    ...
}

Be it a static method or final variable or both, makes no difference. 无论是static方法还是final变量,或者两者都没有关系。 The scope of a local variable means that this assignment will happen every time (except for when it's optimised*). 局部变量的范围意味着该赋值将每次都发生(优化时除外)。

However, when it comes to the strings in your example, the strings will come from the string pool . 但是,在示例中,字符串将来自字符串池 So the assignments happen each time, but they will all reference the same string instance. 因此,分配每次都会发生,但是它们都将引用相同的string实例。

*Optimisation - The JIT compiler may inline the values, so each solution may run exactly as fast as another. *优化-JIT编译器可以内联这些值,因此每种解决方案的运行速度都可以与另一种完全一样。 So you should not try to micro optimize yourself unless you are aware of an actual performance problem. 因此,除非您意识到实际的性能问题,否则不要尝试微优化自己。

In Java are unmodified method variables, that lack the final, qualifier re-initialized each time 在Java中是未修改的方法变量,它们每次都缺少最终的限定符重新初始化

(that the method is called) - of course they are, otherwise they would be uninitialized. (该方法被调用)-当然可以,否则它们将未被初始化。

I think what you really wanted to ask was, can this initialisation be optimized out in some way? 我认为您真正想问的是,可以某种方式优化此初始化吗? If the variable is assigned a constant value, then yes; 如果为变量分配了常量值,则为是;否则,为0。 the JIT compiler might remove the variable altogether, and replace references to it with its (known constant) value. JIT编译器可能会完全删除该变量,并用其(已知常量)值替换对其的引用。

would the final qualifier allow Java to perform an optimization and store the method variable only once? 最终限定符是否将允许Java执行优化并只将方法变量存储一次?

No. A final -qualified local variable can not have its value changed, but each invocation of a method has its own copy of the variable, and different instantiations might use different values for the same final variable. 不能。 final限定的局部变量不能更改其值,但是方法的每次调用都有其自己的变量副本,并且不同的实例化可能对同一final变量使用不同的值。 Consider: 考虑:

void someMethod()
{
   final int number = new Random().nextInt();
}

The value cannot be assigned only once during program execution, because it is different each time the method is called. 该值在程序执行期间不能仅分配一次,因为每次调用该方法都不同。

The final modifier on a local variable has little or no direct effect on optimisation. 局部变量的final修饰符对优化几乎没有或没有直接影响。 It only places restrictions on what can be done with the variable - restrictions which might be followed for any variable, regardless of final qualification - and this in turn allows certain optimisations. 它仅对变量可以执行的操作施加了限制-任何变量都可以遵循这些限制,而与final限定无关,而这又允许进行某些优化。 If the optimizer is worth its salt, it won't care if the variable is declared final and will instead consider if it is effectively constant. 如果优化程序是值得的,那么它不会在乎该变量是否声明为final ,而是会考虑其是否有效地保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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