Efficient loop through Java List

前端 未结 6 1011
花落未央
花落未央 2021-02-07 09:51

The following list is from the google I/O talk in 2008 called \"Dalvik Virtual Machine Internals\" its a list of ways to loop over a set of objects in order from most to least e

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 10:22

    In case of Android, this is the video from Google Developers in 2015.

    To Index or Iterate? (Android Performance Patterns Season 2 ep6) https://www.youtube.com/watch?v=MZOf3pOAM6A

    They did the test on DALVIK runtime, 4.4.4 build, 10 times, to got average results. The result shows "For index" is the best.

    int size = list.size();
    for (int index = 0; index < size; index++) {
        Object object = list.get(index);
        ...
    }

    They also suggest to do similar test by yourself on your platform at the end of the video.

提交回复
热议问题