gpt4 book ai didi

java - 为什么进行类(class)期末考试会加快速度?

转载 作者:行者123 更新时间:2023-12-01 15:35:31 25 4
gpt4 key购买 nike

Possible Duplicate:
Why defining class as final improves JVM performance?

我的项目中有以下类(class):

public final class LinkNode
{
public int value;
public LinkNode next;

public LinkNode(int value, LinkNode next)
{
this.value = value;
this.next = next;
}

public LinkNode(int value)
{
this(value, null);
}
}

我的代码中最慢的一行(总体来说相当复杂)是我构造新的 LinkNode 的地方。 s。

我类的时候发现final ,代码运行速度显着加快。这是为什么?

在这个类中我还能做些什么来优化这个类,特别是主构造函数吗?

(例如,getter/setter 比公共(public)字段更快吗?我可以添加其他修饰符吗?等等。)

最佳答案

I found that when I made the class final, the code ran significantly faster. Why is that?

我认为这是因为你们的基准测试方法存在缺陷。

假设您正在使用最新的 Hotspot JRE,JIT 编译器足够智能,类上的 final 修饰符不会对性能产生影响...除非您实际上使用该类的子类在您的应用程序中。

如果您需要确认,请发布您的方法的详细信息,包括基准代码和基准结果。

关于java - 为什么进行类(class)期末考试会加快速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8922099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com