将方法的参数标记为 Final 是否会使方法调用更快?
我见过以这种方式编写的时间敏感的回溯程序,我想它使编译器避免一些内存复制并进行更快的方法调用,我想这对于递归程序很有用。
但这是我的猜测,我想要对此进行详细的解释/文章或反驳。
I have seen time-sensitive backtracking programs written this way and I guess it makes the compiler avoid some memory copy and make a faster method call, and I guess this is useful on recursive programs.
But this is speculation by me, I'd like a detailed explanation/article on this or a refutation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它对性能的影响为零——事实上,它根本没有运行时影响。
如果您编译一个包含 2 个方法的类 - 一个方法的参数标记为 Final,另一个没有 - 然后查看为每个方法生成的字节码,您会注意到没有任何区别(除了方法姓名)。
final 关键字在此上下文中所做的所有作用就是使您无法在方法内重新分配该变量。
It has zero impact on performance - indeed, it has no runtime effect at all.
If you compile a class that contains 2 methods - one with the parameters marked as final, and the other without - and then look at the bytecode that gets generated for each method, you'll note that there is no difference (other than the method name).
All the final keyword does in this context is make it so that you cannot reassign that variable within the method.