DSA Notes
Master fast I/O in competitive programming — why cin/cout is slow, how sync_with_stdio and tie affect speed, custom fast readers in C++, Python stdin tricks, complete templates for C++ and Python with all optimizations, and when I/O speed is the difference between AC and TLE.
Why I/O Speed Matters
In competitive programming, your solution must complete within a strict time limit — usually 1-3 seconds. For problems with large inputs (n = 10^5 to 10^6), the time spent just reading input can consume a significant portion of your time budget.
Concrete example:
| Slow C++ cin | ~2.0 seconds |
| Fast C++ (sync off) | ~0.2 seconds |
| Custom fast reader | ~0.1 seconds |
| Python built-in input() | ~3.0 seconds for 10^6 integers |
| Python sys.stdin | ~0.5 seconds |
If your algorithm is O(n log n) in 0.5 seconds but I/O takes 2 seconds — you TLE. This is why fast I/O is a fundamental skill.
Complete C++ Template
Custom Fast Reader (Fastest Possible C++)
For extreme cases (n = 10^7 or more) where even optimized cin is too slow:
This is typically 2-3x faster than optimized cin for very large inputs.
Python Fast I/O
The Problem with Python's input()
# SLOW: input() is the standard function
n = int(input())
arr = list(map(int, input().split()))For n = 10^5, this might be fine. For n = 10^6, it's often too slow.
Solution: Use sys.stdin
This is 3-5x faster than the default input().
Even Faster: Read All at Once
Fastest: sys.stdin.buffer (Binary Mode)
Binary mode avoids string decoding overhead — fastest pure Python I/O.
Complete Python Template
Output Optimization
C++ Output
// SLOW: Multiple small writes
for (int x : arr) cout << x << ' ';
// FAST: Build string first, write once
string out;
out.reserve(arr.size() * 12);
for (int x : arr) {
out += to_string(x);
out += ' ';
}
cout << out;
// Or use printf for formatted output (still fast without sync)
// But don't mix with cin after sync_with_stdio(false)!Python Output
# SLOW: print() inside a loop with large output
for x in arr:
print(x)
# FAST: Join and print once
sys.stdout.write('\n'.join(map(str, arr)) + '\n')
# Or:
print(*arr, sep='\n') # Still better than printing individuallyCommon I/O Patterns in Competitive Programming
When I/O Optimization Is Critical
| n ≤ 10^4 | Any I/O method works fine |
| n ≤ 10^5 | Standard cin/input() with basic optimization |
| n ≤ 10^6 | MUST use fast I/O (sync_with_stdio, sys.stdin) |
| n ≤ 10^7 | Custom fast reader in C++; Python may TLE regardless |
| n ≤ 10^8 | C++ custom reader; Python is unlikely to pass |
Summary
Fast I/O is a low-effort, high-reward optimization that every competitive programmer must implement automatically:
C++:
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Use '\n' not endlPython:
These two changes take 5 seconds to type and can prevent TLE on large-input problems. Add them to every solution as a reflex.
*Next Lesson: Contest Strategies*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Fast Input/Output — Why I/O Speed Matters and Complete Templates for C++ and Python.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, competitive, programming, fast
Related Data Structures & Algorithms Topics