백준 문제를 풀다 보면 시간 초과 에러가 나곤 한다.
이때 아래 코드를 참조해 입출력 속도를 줄인다면 도움이 될 듯 하다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ios::sync_with_stdio(false); | |
cin.tie(NULL); | |
cout.tie(NULL); |
위 코드는 C와 C++ 사이의 stream 동기화를 끊는다.
그로 인해 C++ stream은 독립적인 buffer를 갖게 되고, buffer의 수가 감소해 처리 속도가 빨라지게 된다.
단, 위 코드를 사용했을 때 C의 입출력 printf, scanf 등을 사용하면 안된다.
추가로 endl은 buffer를 비우는 역할까지 하기에 약간의 딜레이가 발생한다.
따라서 \n으로 줄바꿈을 한다면 약간의 시간 단축을 기대할 수 있다.