开发者

Always Use Final?

I have read that making some开发者_如何学Pythonthing final and then using it in a loop will bring better performance, but is it good for everything? I have lots of places where there isnt a loop but I add final to the local variables. Does it make it slower or is it still good?

Also there are some places where I have a global variable final (e.g. android paint), does it mean I don't have to make it a local final when using it in loops?


The first thing you should consider is; What is the simplest and clearest way I can write this code. Often this performs well.

final local variables is unlikely to affect performance much. They can help clarity when you have long methods, but I would suggest breaking up method is a better approach.

final fields can affect performance to small degree, but a better reason to make it final is to make it clear that this field never changes (which also helps the JIT)


Don't think about performance. final on object member (fields) have significant memory semantics that may improve performance (but more importantly, its often necessary to make the code correctly work at all). You should always put final on object members whenever you can. For local variables however, you should only use it if it will improve code readerability, or can prevent bugs when a maintainer touches your code.

The general consensus of the Java community is that final on every local variables will make the code difficult to read. On the performance front, you can expect no optimization as local variables are easy to analyze for the compiler. In other words, the compiler can figure it out by itself.


From my experience most variables could be declared final.

However, it looks very ugly. That is my main point against it.

And if the part of the program is not performance critical, beware of premature optimization.


It's considered good form to use final where possible (for fields and variables, not classes and methods), if for no other reason than it makes testing easier. Final will never have a negative impact on performance.


Here are my 2 cents:

Use final on attributes to minimize mutability and for documentation purposes, only use final on local variables if they are used in inner/anonymous classes.

DON'T use it for microoptimizations! Especially don't use them on classes or methods because you think it will improve performance. Make classes and methods final to prohibit inheritance or overriding methods.


Final on attributes should not have any impact on performance. Except: in a multi threaded environment where several threads access the same field and "don't know" if they have to relaod it. Final on local variables has no impact at all, as nothing except the local scope can access them anyway. Final on methods can have an impact during JIT compiling. If a method is final and small the compiler can inline it in loops, as it is clear that no one will have overwritten it. I usually don't use final on attributes at all, as final attributes can not be loaded from DBs easily etc. Declaring pararameters to methods final lokos ugly (I never assign to them inside my code anyway) but might prevent simple bugs comming from typoes. However if you start using proper names for your variables you unliek make such typoes.


Theoretically, if you make a local variable final it can be optimized. I don't think making them final yourself really improves performance though, because the optimizer probably already detects when your locals don't change. That said, it can't hurt to help it a bit.

In some situations, it would help to change one variable into two, e.g. from this

String a = "foo";
if (lol) a += "bar";

for(.. 1000 ...) doSomething(a);

to

final String a;
{
    String ma = "foo";
    if (lol) ma += "bar";
    a = ma;
}

for(.. 1000 ...) doSomething(a);

Disclaimer: I'm not a JIT expert.


Final variables are constants, therefore the compiler could generate constant value instead of variable referencing instruction. Of course that would improve speed (and commonly size as well).

Also there are some places where I have a global variable final (e.g. android paint), does it mean I don't have to make it a local final when using it in loops?

Sorry, do you mean you don't have to:

final int somefinalvalue = 0;

void amethod() {
  final int somefinalvalue = 0; // repeated from global one
}

or what? remember that if you declare local variable which has the same name as global one, that would 'shadow' the global one. i.e. it's actually a totally different variable. if you already have the global one, just use that. no need to re-declare.


I don't think this should be your first concern, as mentioned by @perter-lawrey. First, compiler optimization can very much do the trick; second, there are some tools that can analyze your generated class files and do the same thing, for example, ProGuard: java shrinker, optimizer, obfuscator, and preverifier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜