总是使用 Final?
我读过,将某些东西做成最终的,然后在循环中使用它会带来更好的性能,但这对一切都有好处吗?我有很多地方没有循环,但我将 Final 添加到局部变量中。它会使速度变慢还是仍然很好?
还有一些地方我有一个全局变量final(例如android Paint),这是否意味着我在循环中使用它时不必将其设为本地final?
I have read that making something 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您应该考虑的第一件事是;我编写这段代码的最简单、最清晰的方法是什么?通常这会表现良好。
final
局部变量不太可能对性能产生太大影响。当你有很长的方法时,它们可以帮助你弄清楚,但我建议分解方法是更好的方法。final
字段可能会在很小程度上影响性能,但将其设置为final
的更好原因是明确该字段永远不会更改(这也有助于 JIT)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 itfinal
is to make it clear that this field never changes (which also helps the JIT)不要考虑性能。对象成员(字段)上的
final
具有重要的内存语义,可以提高性能(但更重要的是,它通常是使代码正确工作所必需的)。只要有可能,您就应该始终将final
放在对象成员上。然而,对于局部变量,只有当它能够提高代码可读性,或者可以防止维护人员接触代码时出现错误时,才应该使用它。Java 社区的普遍共识是,每个局部变量上的
final
都会导致代码难以阅读。在性能方面,您可以预期不会有任何优化,因为编译器很容易分析局部变量。换句话说,编译器可以自己计算出来。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 putfinal
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.根据我的经验,大多数变量都可以声明为
final
。然而,它看起来非常难看。这是我反对的主要观点。
如果程序的这一部分不是性能关键的部分,请注意过早的优化。
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.
尽可能使用 Final 被认为是一种很好的形式(对于字段和变量,而不是类和方法),如果没有其他原因,只是它使测试更容易。 Final永远不会对性能产生负面影响。
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.
这是我的 2 美分:
在属性上使用 Final 可以最大程度地减少可变性,并且出于文档目的,仅在局部变量用于内部/匿名类中时才在局部变量上使用 Final。
不要将其用于微优化!特别是不要在类或方法上使用它们,因为您认为这会提高性能。将类和方法设为final 以禁止继承或重写方法。
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 属性不应对性能产生任何影响。例外:在多线程环境中,多个线程访问同一字段并且“不知道”是否必须重新加载它。 Final 对局部变量完全没有影响,因为除了局部作用域之外没有任何东西可以访问它们。
Final on 方法可能会在 JIT 编译期间产生影响。如果一个方法是最终的并且很小,编译器可以将其内联到循环中,因为很明显没有人会覆盖它。
我通常根本不会在属性上使用 Final,因为 Final 属性无法轻松地从数据库加载等。将参数声明为方法 Final Lokos 很难看(无论如何我都不会在代码中分配给它们),但可能会防止因拼写错误而出现的简单错误。但是,如果您开始为变量使用正确的名称,您就不会犯这样的拼写错误。
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.
理论上,如果将局部变量设为final,它就可以被优化。我不认为自己将它们定为最终版本确实可以提高性能,因为优化器可能已经检测到本地变量何时没有改变。也就是说,帮助它一点也不会有什么坏处。
在某些情况下,将一个变量更改为两个变量会有所帮助,例如从这里
到
免责声明:我不是 JIT 专家。
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
to
Disclaimer: I'm not a JIT expert.
Final变量是常量,因此编译器可以生成常量值而不是变量引用指令。当然,这会提高速度(通常也会提高尺寸)。
抱歉,您的意思是您不必:
还是什么?请记住,如果您声明与全局变量同名的局部变量,则会“隐藏”全局变量。即它实际上是一个完全不同的变量。如果你已经有了全局的,就使用它。无需重新声明。
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).
Sorry, do you mean you don't have to:
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.
正如 @perter-lawrey 所提到的,我认为这不应该是您首先关心的问题。首先,编译器优化可以起到很大的作用;其次,有一些工具可以分析生成的类文件并执行相同的操作,例如 ProGuard:java Shrinker、optimizer、混淆器和预验证器。
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.