python

[python] sys.stdin.readline vs. input 차이

감자156 2023. 8. 14. 13:00
반응형

sys.stdin.readline과 input의 차이
sys.stdin.readline과 input의 차이

 

input( ) :

- 사용자의 input에서 개행문자를 제거한 후 리턴한다.

- 사용자의 prompt를 받아 사용할 수 있다.

name = input("Please enter your name: ")
print("Hello, " + name)

 

sys.stdin.readline( ):

- 사용자의 input을 개행문자까지 그대로 받아온다.

- 인자로 양수값을 받을 수 있는데, 사용자의 input에서 몇 byte(몇 글자)를 받아올지 입력받는다.

 

왜 sys.stdin.readline가 내장함수 input보다 빠를까?

- 예를 들어, 

 

"나는 A다.(\n)

나는 B다. (\n)

나는 C다. (\n)

나는 D다. (\n)"

 

라는 입력을 받을 때,

input()의 경우 각 문장을 \n가 나올 때마다 바로바로 메모리에서 리턴하여 변수에 저장한다.

sys.stdin.readline의 경우 4문장을 모두 입력버퍼에 저장한 후, 필요할 때, 필요한 만큼만 리턴한다.

따라서, 대용량의 데이터를 다룰 때에는 sys.stdin.readline이 더 효율적일 때가 있다.

 

 

ref ) https://www.geeksforgeeks.org/difference-between-input-and-sys-stdin-readline/

 

Difference between input() and sys.stdin.readline() - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

반응형