完整代码见 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 所做的其他操作的耗时,