Introduction: The Speed Paradox That Blew Up Stack Overflow
\n\nImagine this situation: you are solving a competitive programming problem or processing a gigabyte-sized log file. The Python script finishes in 2 seconds. You rewrite it in C++, expecting a tenfold speedup, but… you get 10 seconds! Sound familiar? This very question garnered thousands of views on Stack Overflow and spawned a legendary discussion: “Why is reading lines from stdin much slower in C++ than Python?”.
\n\nHow could “fast” C++ possibly lose to “slow” Python in such a trivial operation as reading lines? The answer lies not in the speed of the language itself, but in the default settings and architecture of iostream. In this article, we will conduct our own investigation, write test code in C++ and Python, measure the time, and, most importantly, learn how to make I/O in C++ truly fast.
\n\nThis article is not dry theory, but a practical guide for everyone who writes in C++ and faces I/O performance issues. Let's go!
\n\nThe Core of the Problem: Why Does iostream “Lag”?
\n\nWhen you write std::cin >> data or std::getline(std::cin, line), C++ by default does something many don't even suspect. It synchronizes its I/O streams with the standard C streams (stdin, stdout). Why? For compatibility: you can use printf and std::cout in the same program simultaneously, and the output won't get mixed up.
This synchronization is expensive. Every call to cin or cout checks the state of the C streams, turning a fast buffered operation into a slow synchronous one.
In Python, it's different. The input() function and sys.stdin.readline() use their own, well-optimized buffering at the C level. No unnecessary synchronization — just reading a block of data from the file descriptor. This is precisely why reading from stdin in Python can be faster than in C++ “out of the box”.
Experiment: Speed Test
\n\nLet's test this in practice. Let's write a simple program that reads 1,000,000 lines from stdin and does nothing with them (just reads them). First in C++ “as is”:
\n\n// slow_cin.cpp\n#include <iostream>\n#include <string>\n\nint main() {\n std::ios::sync_with_stdio(false); // Let's comment this out for now\n std::cin.tie(nullptr); // And this\n\n std::string line;\n while (std::getline(std::cin, line)) {\n // Do nothing\n }\n return 0;\n}\n\nAnd now in Python:
\n\n# fast_python.py\nimport sys\n\ndef main():\n for line in sys.stdin:\n pass\n\nif __name__ == "__main__":\n main()\n\nLet's run the test with seq 1000000 | ./slow_cin and seq 1000000 | python3 fast_python.py. The result will be shocking: C++ without optimizations can be 5-10 times slower than Python! But don't rush to bury C++ — the problem is solved with two lines of code.
Solution: Two Magic Lines
\n\nAll you need to speed up I/O in C++ is to disable synchronization and stream tying. Add this to the beginning of main():
std::ios::sync_with_stdio(false);\nstd::cin.tie(nullptr);\n\nThe first line disables synchronization between iostream and stdio. The second unties cin from cout (by default, before each input operation, cout flushes its buffer, which also slows things down). After this, your C++ code will start performing on par with Python, and often faster.
Checking the Result
\n\nNow uncomment these lines in our test and run it again. You will see that C++ performance skyrockets. In some benchmarks, the difference reaches 10x.