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)) {
No answers yet.
Log in to leave an answer
Blogs
Concurrency and Multithreading in Go: A Complete Guide for Developers
Monitoring JavaScript Applications: Advanced Metrics and Alerts
Profiling C++ Code: Finding Bottlenecks in Practice
How to befriend std::string with const char*: a complete guide for beginners