1

Java 8 Lambda 性能测试

 1 year ago
source link: https://www.jansora.com/notebook/107544
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Java 8 Lambda 性能测试闲文杂记Java 8 Lambda 性能测试 导航前往主站应用闲文杂记代码在线代码魔方房价浅析

完整代码见 https://github.com/Jansora/demo/tree/master/backend/spring-boot/demo/src/main/java/com/jansora/demo

public class LambdaDemo extends AbstractDemoFactory {

    private static final DoSomething doSomething = () -> {

    };

    @Override
    protected DoSomethingWithThrowable doSomething(String[] args) throws Throwable {
        return () -> {
            // call 10 times
            for (int j = 0; j < 10; j++) {

                Cost.time("normal 1000 0000", () -> {
                    for (int i = 0; i < 1_000_000; i++) {

                    }
                });

                Cost.time("normal 1000 0000", () -> {
                    for (int i = 0; i < 1_000_000; i++) {
                        doSomething.doSomething();
                    }
                });
            }

        };
    }
}


2022-07-21 15:33:01.979  [ --- COST --- ] com.jansora.demo.lambda.LambdaDemo cost start 
2022-07-21 15:33:01.979  [ --- COST --- ] LambdaDemo cost start 
2022-07-21 15:33:01.979  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.980  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.861009 ms. ] 
2022-07-21 15:33:01.980  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.982  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 1.877455 ms. ] 
2022-07-21 15:33:01.983  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.983  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.219573 ms. ] 
2022-07-21 15:33:01.983  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.983  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.219965 ms. ] 
2022-07-21 15:33:01.983  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.21822 ms. ] 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.218417 ms. ] 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.218201 ms. ] 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.218264 ms. ] 
2022-07-21 15:33:01.984  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.218212 ms. ] 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.227684 ms. ] 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.284417 ms. ] 
2022-07-21 15:33:01.985  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.986  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.230869 ms. ] 
2022-07-21 15:33:01.986  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.986  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.222295 ms. ] 
2022-07-21 15:33:01.986  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.220048 ms. ] 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.220665 ms. ] 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.219826 ms. ] 
2022-07-21 15:33:01.987  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.222573 ms. ] 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.220303 ms. ] 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.227021 ms. ] 
2022-07-21 15:33:01.988  [ --- COST --- ] normal 1000 0000 cost start 
2022-07-21 15:33:01.989  [ --- COST --- ] normal 1000 0000 cost end. [ cost: 0.218281 ms. ] 
2022-07-21 15:33:01.989  [ --- COST --- ] LambdaDemo cost end. [ cost: 9.380338 ms. ] 
2022-07-21 15:33:01.989  [ --- COST --- ] com.jansora.demo.lambda.LambdaDemo cost end. [ cost: 9.919391 ms. ] 

结论: 1000W 此调用性能损耗在 0.01 ms 一下. 性能基本无差.

但更需要警惕的是为了适配 lambda 所做的其他操作的耗时,


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK