티스토리 뷰

728x90
백준 풀면서 궁금했던 것.
왜 Scanner는 BufferedReader보다 느린 것인가..?
어떤 원리로 돌아가는지를 모르니
왜 빠른지, 느린지도 모르고 일단 쓰고는 있었음.
궁금하니까 한 번 파본다

 

 

 

Scanner

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

 

Scanner (Java Platform SE 8 )

Scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that match

docs.oracle.com

여그서 발췌한 내용이다.

즉, Scanner 클래스는 입력값이 들어오면 그걸 읽을 때
정규식을 사용하고,
공백 따라서 입력된 내용을 토큰 단위로 나누고,
토큰 따라서 또 변환하고...
이렇게 입력값을 처리 한댄다.


왜 오래 걸렸는지 BufferedReader 안봐도 알 것 같은데...?
그래도 보자고

BufferedReader

https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html

 

BufferedReader (Java Platform SE 8 )

Reads characters into a portion of an array. This method implements the general contract of the corresponding read method of the Reader class. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read

docs.oracle.com

Buffer 라는 것을 이용해서 stream을 타고 온 애들을 담고 있다가 한 번에 파팟 보낸다는 것이여
Scanner랑은 다르게 뭐 입력할 때마다 정규화니 토큰화니 나 빼고,
버퍼 풀이거나 개행문자 등장하면 그 때 처리하는 거지
훨~씬 빠르게 생겼다야

겉핥기로나마 이해 완.

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함