gpt4 book ai didi

java - 为什么 byte 比 int 慢?类实例化

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:47:44 26 4
gpt4 key购买 nike

我有这个简单的代码。如果我在 MyClass 中将 int 更改为 byte,那么由于某种原因它的工作速度会慢 1.5。有什么想法,为什么?

public class Test {

public static void main(String[] args) {
double start = System.currentTimeMillis();
MyClass[] arr = new MyClass[10_000_000];

for (int i = 0; i < arr.length; i++) {
arr[i] = new MyClass();
}

double end = System.currentTimeMillis();
System.out.println(end-start);
}
}
class MyClass {
final public int pole = 50;
int eshePole;
}

最佳答案

很难相信这样的测试结果,因为您没有进行任何预热以允许 JVM 执行优化代码路径等操作。值得一看的文章如下:

https://wikis.oracle.com/display/HotSpotInternals/MicroBenchmarks

您的测试也不正确,因为 System.currentTimeMillis() 返回的是 long,而不是 double。

关于java - 为什么 byte 比 int 慢?类实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493613/

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