Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream의 단점은? #2

Open
sendkite opened this issue Aug 3, 2022 · 3 comments
Open

Stream의 단점은? #2

sendkite opened this issue Aug 3, 2022 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@sendkite
Copy link
Contributor

sendkite commented Aug 3, 2022

주제

Stream은 일단 쓰는게 좋을까? 언제 쓰면 좋고, 언제 쓰면 안될까?

선정 배경

Stream 안쓰는 코드도 얼마든지 있는데 굳이 안변경하고 내버려두는 이유는 뭘까?

본론

@sendkite sendkite added the question Further information is requested label Aug 3, 2022
@sendkite sendkite self-assigned this Aug 3, 2022
@sendkite
Copy link
Contributor Author

sendkite commented Aug 4, 2022

데이터가 수천건이 넘어가면, 일반 for 문이 성능이 더 좋게 나온다.

왜 인지 명확히 모르는 상태..

@sendkite
Copy link
Contributor Author

sendkite commented Aug 4, 2022

  1. stream은 한번 consumed 되면 다시 재사용 할 수 없다. 사용을 위해서 매번 streams을 만든다.

  2. 자바의 람다, 익명 클래스만 중괄호 범위 안의 final 변수에 접근할 수 있다.

  3. 만약 parallel stream을 사용할때, 람다표현식이 stateful하면 random한 결과 값을 응답할 수 있다.

출처 : 자바 doc

@thalals
Copy link
Member

thalals commented Aug 16, 2022

for 문의 성능이 Stream 보다 좋은 이유

  1. for loop 의 경우 오래된 만큼 자바 자체에서 최적화가 굉장히 잘되어있다.
  2. Stream 의 경우 비교적 최근에 나와 최적화가 덜 된 상태
  3. Paraller Stream을 사용하면 단순 for 문과의 성능차이가 크게 나지 않느다.
  4. 단순 index 비교가아닌, 조건문 비교라면 Stream 이 더 빠르다.
    고 합니다 :)

https://jypthemiracle.medium.com/java-stream-api%EB%8A%94-%EC%99%9C-for-loop%EB%B3%B4%EB%8B%A4-%EB%8A%90%EB%A6%B4%EA%B9%8C-50dec4b9974b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants