java list最优遍历

java list最优遍历

android 官方推荐除了ArrayList,别的collections 使用增强LOOP ,也就是foreach ArrayList 使用手写计数loop without size

以下为官方原文

static class Foo {
    int mSplat;
}

Foo[] mArray = ...

public void zero() {
    int sum = 0;
    for (int i = 0; i < mArray.length; ++i) {
        sum += mArray[i].mSplat;
    }
}

public void one() {
    int sum = 0;
    Foo[] localArray = mArray;
    int len = localArray.length;

    for (int i = 0; i < len; ++i) {
        sum += localArray[i].mSplat;
    }
}

public void two() {
    int sum = 0;
    for (Foo a : mArray) {
        sum += a.mSplat;
    }
}
复制代码

zero() is slowest, because the JIT can’t yet optimize away the cost of getting the array length once for every iteration through the loop.

one() is faster. It pulls everything out into local variables, avoiding the lookups. Only the array length offers a performance benefit.

two() is fastest for devices without a JIT, and indistinguishable from one() for devices with a JIT. It uses the enhanced for loop syntax introduced in version 1.5 of the Java programming language.

So, you should use the enhanced for loop by default, but consider a hand-written counted loop for performance-critical [ArrayList](https://developer.android.google.cn/reference/java/util/ArrayList.html) iteration.

转载于:https://juejin.im/post/5c8755b35188257ddb6afd36

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101056.html原文链接:https://javaforall.net

(0)
上一篇 2021年7月2日 下午1:00
下一篇 2021年7月2日 下午2:00


相关推荐

  • vue 富文本插件_前端富文本编辑器插件

    vue 富文本插件_前端富文本编辑器插件1.引用quill&lt;scriptsrc="/lib/quill/quill.min.js"&gt;&lt;/script&gt;&lt;linkhref="/lib/quill/quill.snow.css"rel="stylesheet"/&gt;2.RichEditorDemo.vue&lt;template&gt;&lt;divcl

    2022年10月14日
    4
  • vim怎么搜索字符串_进行字符串查找

    vim怎么搜索字符串_进行字符串查找1. 命令模式下,输入:/字符串比如搜索user,输入/user按下回车之后,可以看到vim已经把光标移动到该字符处和高亮了匹配的字符串2. 查看下一个匹配,按下n(小写n)3. 跳转到上一个匹配,按下N(大写N)4. 搜索后,我们打开别的文件,发现也被高亮了,怎么关闭高亮?    命令模式下,输入:n

    2026年2月23日
    5
  • 施密特触发器原理图解详细分析

    施密特触发器原理图解详细分析转自 https blog csdn net chentengkui article details 重要特性 施密特触发器具有如下特性 输入电压有两个阀值 VL VH VL 施密特触发器通常用作缓冲器消除输入端的干扰 nbsp 施密特波形图 nbsp 施密特触发器也有两个稳定状态 但与一般触发器不同的是 施密特触发器采用电位触发方式 其状态由输入信号电位维持 对于负向递减和

    2026年3月19日
    2
  • IDEA 2021 mybatis log plugin 激活【2021免费激活】

    (IDEA 2021 mybatis log plugin 激活)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1435QFILVV-eyJsa…

    2022年3月30日
    378
  • file write error怎么解决_internal error 28013

    file write error怎么解决_internal error 28013(原創) 如何解決Nios II EDS的『Error parsing project STF file』錯誤訊息? (SOC) (Nios II)

    2022年4月21日
    135
  • c语言socket编程流程,C语言socket编程简单步骤「建议收藏」

    c语言socket编程流程,C语言socket编程简单步骤「建议收藏」服务器端/***************************************函数功能:创建套接字的函数*输入参数:无*输出参数:无*返回值:套接字的ID*/intcreat_socket(){intret;ret=socket(AF_INET,SOCK_STREAM,0);if(ret==-1){perror(“socketerror”);exit(1);}printf(“创建s…

    2022年7月13日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号