在Java中迭代数组的最快方法

在Java中迭代数组的最快方法

本文介绍了在Java中迭代数组的最快方法:循环变量vs增强语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,以旧式方式迭代数组是否更快,

In Java, is it faster to iterate through an array the old-fashioned way,

for (int i = 0; i < a.length; i++)
    f(a[i]);

或使用更简洁的表格,

for (Foo foo : a)
    f(foo);

对于ArrayList,答案是否相同?

For an ArrayList, is the answer the same?

当然,对于大量的应用程序代码,答案是它没有明显的区别,因此应该使用更简洁的表单来提高可读性。然而,我所关注的背景是重型技术计算,必须执行数十亿次操作,因此即使很小的速度差异也可能最终显着。

Of course for the vast bulk of application code, the answer is it makes no discernible difference so the more concise form should be used for readability. However the context I'm looking at is heavy duty technical computation, with operations that must be performed billions of times, so even a tiny speed difference could end up being significant.

推荐答案

如果你在数组中循环,那应该没关系 - 增强的for循环无论如何都使用数组访问。

If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

例如,请考虑以下代码:

For example, consider this code:

public static void main(String[] args)
{
    for (String x : args)
    {
        System.out.println(x);
    }
}

使用 javap进行反编译时 - c测试我们得到(对于方法):

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   astore_1
   2:   aload_1
   3:   arraylength
   4:   istore_2
   5:   iconst_0
   6:   istore_3
   7:   iload_3
   8:   iload_2
   9:   if_icmpge   31
   12:  aload_1
   13:  iload_3
   14:  aaload
   15:  astore  4
   17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  aload   4
   22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  iinc    3, 1
   28:  goto    7
   31:  return

现在更改它以使用显式数组访问:

Now change it to use an explicit array access:

public static void main(String[] args)
{
    for (int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

这个反编译为:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge   23
   8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

增强型for循环中有更多的设置代码,但它们基本上做同样的事情。没有涉及迭代器。此外,我希望他们能够使用更多相似的代码进行JIT。

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

建议:如果你真的认为它可能会产生重大影响(它只会永远如果循环的主体是绝对微不足道的话,那么你应该将它与你的真实应用程序进行基准测试。这是唯一重要的情况。

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

这篇关于在Java中迭代数组的最快方法:循环变量vs增强语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:14