02. Fast I/O in Python
input() and print() are slow enough to cause TLE on their own when N is large. Master the idioms for fast I/O — sys.stdin.readline for reading, sys.stdout.write or '\n'.join() for writing.
What you'll learn
- 1Explain why input() is slow and replace it with sys.stdin.readline
- 2Speed up output with sys.stdout.write or print('\n'.join(...))
- 3Read multiple input formats: split, map(int, ...), EOF loops
Overview
Many Python solutions TLE not because the algorithm is wrong but because input() and print() are slow. Switching to sys.stdin.readline can cut runtime 5–10×. Memorize the idioms in this lecture and apply them by default.
Core Concepts
1. input() vs sys.stdin.readline
input() includes overhead like prompt handling and removing the trailing newline. sys.stdin.readline is much closer to the raw read syscall.
# Slow
n = int(input())
# Fast
import sys
input = sys.stdin.readline # shadow built-in
n = int(input())sys.stdin.readline keeps the trailing newline. Use .rstrip() for strings, but int() ignores it so you don't need to strip for numbers.
2. Common reading patterns
import sys
input = sys.stdin.readline
# One int
n = int(input())
# Two ints on one line
a, b = map(int, input().split())
# A list of ints (space-separated)
nums = list(map(int, input().split()))
# 2D matrix (n rows, m cols)
mat = [list(map(int, input().split())) for _ in range(n)]
# All remaining lines at once
data = sys.stdin.read().split()3. Fast output
Calling print() in a loop is slow. Either join results once or write directly to sys.stdout.
import sys
output = []
for v in answers:
output.append(str(v))
sys.stdout.write('\n'.join(output) + '\n')
# Or:
print('\n'.join(map(str, answers)))4. EOF (unknown number of lines)
import sys
for line in sys.stdin:
a, b = map(int, line.split())
print(a + b)Examples
Example 1 — src/01_input_basics.py
All the reading idioms in one runnable script (single int, two ints, list, 2D).
Example 2 — src/02_fast_io_compare.py
Time how long input() vs sys.stdin.readline takes for 100,000 lines.
Example 3 — src/03_eof_loop.py
Read until end-of-file when the problem doesn't tell you the line count up front.
Example 4 — src/04_output_batch.py
Buffer thousands of answers and write once at the end with sys.stdout.write.
Common Mistakes
- Forgetting `import sys` before `sys.stdin.readline`.
- Calling sys.stdin.readline() repeatedly in a hot loop instead of caching `input = sys.stdin.readline`.
- Printing inside a loop with hundreds of thousands of iterations — collect into a list, then '\n'.join.
- Forgetting .rstrip() when readline returns a trailing newline and the comparison fails.
- Mixing input() and sys.stdin.readline in the same program — pick one.
Recap
- Default to `input = sys.stdin.readline` at the top of every BOJ solution.
- Read all-at-once with `sys.stdin.read().split()` when you have many tokens.
- Buffer output and write once.
Try It
- Solve BOJ 15552 (A+B - 7) which is the canonical fast-I/O lesson.
- Take an old solution of yours and time it before/after switching to fast I/O.
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗