Avatar ·

Why reading lines from stdin in C++ is slower than in Python?

Hi everyone! I'm a beginner C++ developer who switched from Python, and I've encountered an unexpected problem. In Python, I often solved problems where I needed to read large amounts of data from standard input (for example, 10 million lines). Python code using `sys.stdin.readline()` worked quite fast. But when I tried to write similar code in C++ using `std::getline` and `std::cin`, the performance turned out to be several times worse. I wrote a simple benchmark: read all lines from stdin and simply count them. Here's the C++ code I'm using: ```cpp #include #include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::string line; int count = 0; while (std::getline(std::cin, line)) {