Java Lambda 会影响性能吗?

109 阅读2分钟

测试代码LamdaTest.java

import java.util.*;

class LamdaTest {
    static volatile List<Integer> integers = new ArrayList<Integer>();

    // 普通 for 循环测试
    public static int forLoopInteger() {
        int total = 0;
        for (int i = 0; i < integers.size(); i++) {
            total += integers.get(i);
        }

        System.out.println("total: " + total);
        return total;
    }

    // forEach 循环测试
    public static int forOfInteger() {
        int total = 0;
        for (Integer n : integers) {
            total += n;
        }
        System.out.println("total: " + total);
        return total;
    }

    // lamda map循环测试
    public static int lambdaInteger() {
        int total = 0;
        integers.forEach(n -> {
            // System.out.println("n:" + n);
        });
        // total = integers.stream().reduce(0, (a, b) -> {
        // return a + b;
        // });
        System.out.println("total: " + total);
        return total;
    }

    public static void main(String[] args) {
        // 初始化integers,填充内容
        for (int i = 0; i < 100000; i++) {
            integers.add(i);
        }

        long startTime = 0L;
        long costTime = 0L;

        System.out.println("forLoopInteger start: ");
        startTime = System.currentTimeMillis();
        forLoopInteger();
        costTime = System.currentTimeMillis() - startTime;
        System.out.println("forLoopInteger cost: " + costTime);

        System.out.println("====");

        startTime = System.currentTimeMillis();
        System.out.println("forOfInteger start: ");
        forOfInteger();
        costTime = System.currentTimeMillis() - startTime;
        System.out.println("forOfInteger cost: " + costTime);

        System.out.println("====");

        startTime = System.currentTimeMillis();
        System.out.println("lambdaInteger start: ");
        lambdaInteger();
        costTime = System.currentTimeMillis() - startTime;
        System.out.println("lambdaInteger cost:  " + costTime);
    }
}

/**
 * // 测试1结果
 * java -version
 * java version "1.8.0_401"
 * Java(TM) SE Runtime Environment (build 1.8.0_401-b10)
 * Java HotSpot(TM) 64-Bit Server VM (build 25.401-b10, mixed mode)
 * 
 * 第一组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 6
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 7
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 50
 * 
 * 第二组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 5
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 5
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 46
 * 
 * 第三组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 5
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 7
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 47
 */

/**
 * // 测试2结果
 * java -version
 * openjdk version "11.0.4" 2019-07-16 LTS
 * OpenJDK Runtime Environment 18.9 (build 11.0.4+11-LTS)
 * OpenJDK 64-Bit Server VM 18.9 (build 11.0.4+11-LTS, mixed mode, sharing)
 * 
 * 第一组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 115
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 40
 * ====
 * lambdaInteger start:
 * total: 704982704
 * lambdaInteger cost: 166
 * 
 * 第二组:
 * forLoopMaxInteger start:
 * total: 704982704
 * forLoopMaxInteger cost: 55
 * ====
 * forOfMaxInteger start:
 * total: 704982704
 * forOfMaxInteger cost: 27
 * ====
 * lambdaMaxInteger start:
 * total: 704982704
 * lambdaMaxInteger cost: 31
 * 
 * 第三组:
 * forLoopMaxInteger start:
 * total: 704982704
 * forLoopMaxInteger cost: 51
 * ====
 * forOfMaxInteger start:
 * total: 704982704
 * forOfMaxInteger cost: 29
 * ====
 * lambdaMaxInteger start:
 * total: 704982704
 * lambdaMaxInteger cost: 29
 */

/**
 * // 测试3结果
 * java -version
 * java version "10.0.1" 2018-04-17
 * Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
 * Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
 * 
 * 第一组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 33
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 6
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 7
 * 
 * 第二组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 33
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 6
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 8
 * 
 * 第三组:
 * forLoopInteger start:
 * total: 704982704
 * forLoopInteger cost: 35
 * ====
 * forOfInteger start:
 * total: 704982704
 * forOfInteger cost: 8
 * ====
 * lambdaInteger start:
 * total: 0
 * lambdaInteger cost: 9
 */

结论

通过以上测试,JDK1.8下lambda确实有性能问题,但之后版本lambda做了性能优化,不会有性能问题了,甚至更快。