00-Java数组遍历性能对比

三种遍历方式性能对比

  1. 循环与数组的length比较

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class JavaMain {
    static Object[] objs = new Object[10000000];
    static int zero() {
    int sum = 0;
    for(int i = 0; i < objs.length; i++) {
    sum ^= objs[i].hashCode();
    }
    return sum;
    }
    }
  2. 将数组length存在方法栈中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class JavaMain {
    static Object[] objs = new Object[10000000];
    static int one() {
    int sum = 0;
    int len = objs.length;
    for(int i = 0; i < len; i++) {
    sum ^= objs[i].hashCode();
    }
    return sum;
    }
    }
  3. for-each循环

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class JavaMain {
    static Object[] objs = new Object[10000000];
    static int two() {
    int sum = 0;
    for (Object obj : objs) {
    sum ^= obj.hashCode();
    }
    return sum;
    }
    }

测试速度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class JavaMain {
static Object[] objs = new Object[10000000];
static long run(IntSupplier f) {
long start = System.currentTimeMillis();
int result = 0;
for(int i = 0; i < 100; i++) {
result = f.getAsInt();
}
long end = System.currentTimeMillis();
System.out.println("result = " + result + ", time = " + (end - start) / 1000.0);
return end - start;
}
public static void main(String[] args) {
for (int i = 0; i < objs.length; i++) {
objs[i] = new Object();
objs[i].hashCode(); // 首次计算hashcode更慢,先缓存
}
for (int i = 0; i < 10; i++) {
long zeroTime = run(JavaMain::zero);
long oneTime = run(JavaMain::one);
long twoTime = run(JavaMain::two);
System.out.printf("2比1快: %.2f%%\n", ((double)oneTime - twoTime) / oneTime * 100);
System.out.printf("2比0快: %.2f%%\n", ((double)zeroTime - twoTime) / zeroTime * 100);
System.out.printf("1比0快: %.2f%%\n", ((double)zeroTime - oneTime) / oneTime * 100);
}
}
}

测试结果

1
2
3
4
5
6
result = 360970567, time = 1.393
result = 360970567, time = 1.174
result = 360970567, time = 0.886
2比1快: 24.53%
2比0快: 36.40%
1比0快: 18.65%
阅读更多