Java
[Java] Lambda & Stream 3
quedevel
2020. 2. 26. 14:17
728x90
반응형
마냥 좋다고 생각하고 여기저기 검색중에 이러한 글을 봤다.
=> lambda operations do not allow any external variable operation within itself.
제길... 역시 뭔가 약점은 존재하는군...
성능에서도 for loop와 stream forEach를 비교했을때 더 떨어진다고 한다.
성능 테스트
public class Main {
public static void main(String[] args) {
// for loop와 stream forEach 의 성능 비교
Long forLoopStart = System.currentTimeMillis();
for (int i = -210000000; i < 2100000000; i++) {
for (int j = -210000000; j < 2100000000; j++) {
}
}
Long forLoopEnd = System.currentTimeMillis();
System.out.println("ForLoop: " + (forLoopEnd - forLoopStart));
Long streamStart = System.currentTimeMillis();
IntStream.range(-2100000, 21000000).forEach(i -> {
IntStream.range(-2100000, 21000000).forEach(j -> {
});
});
Long streamEnd = System.currentTimeMillis();
System.out.println("Stream: " + (streamEnd - streamStart));
}
}
stream이 for loop보다 범위도 훨씬 작은데도 불구하고 결과는
ForLoop: 59
Stream: 517
이렇게 나왔다... 실화인가
그래도 정말 성능이 눈에 띄게 안좋을때만 아니면 명시적인 stream을 애용하고 싶다.
728x90
반응형