【问题标题】:Why byte works slower than int? class instantiation为什么 byte 的工作速度比 int 慢?类实例化
【发布时间】:2013-07-05 16:51:15
【问题描述】:

我有这个简单的代码。如果我在 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;
}

【问题讨论】:

  • 如果我在 MyClass 中将 int 更改为 byte,那么由于某种原因它会慢 1.5 倍 .. 你是在谈论 poleeshePole 变量吗?
  • 它工作 1.5 什么慢?
  • @Ingo 慢了 1.5 倍,我假设
  • 它的运行速度慢了 1.5 倍

标签: java performance


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 2023-03-26
    • 2010-10-30
    • 1970-01-01
    • 2020-01-02
    • 2016-04-10
    • 2020-05-11
    相关资源
    最近更新 更多