---
title: "Why is reading lines from stdin much slower in C++ than in Python?"  
description: "Why is reading lines from stdin much slower in C++ than in Python?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/158991/why-is-reading-lines-from-stdin-much-slower-in-c-plus-plus-than-in-python  
category: "python"  
tags: ["python", "programming language"]  
reading_time: 2 minutes  

---

# Why is reading lines from stdin much slower in C++ than in Python?

Why is [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) lines from stdin much slower in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) than in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

There are a few reasons why reading lines from stdin is much slower in C++ than in Python.

- **Python's readline() function is more efficient.** The `readline()` function in Python is a built-in function that is specifically designed to read lines from stdin. The `readline()` function in C++ is not a built-in function, and it is implemented using the `std::cin` object. The `std::cin` object is a general-purpose input stream that can be used to read data from a variety of sources, including stdin. However, the `std::cin` object is not as efficient as the `readline()` function in Python.
- **Python's readline() function is multithreaded.** The `readline()` function in Python is multithreaded, which means that it can read lines from stdin in parallel. The `std::cin` object in C++ is not multithreaded, which means that it can only read lines from stdin sequentially.
- **Python's readline() function is optimized for stdin.** The `readline()` function in Python is optimized for reading lines from stdin. The `std::cin` object in C++ is not optimized for reading lines from stdin.

As a result of these factors, the `readline()` function in Python is much faster than the `std::cin` object in C++.

Here is an example of how to read lines from stdin in Python and C++:

Python

```plaintext
def read_lines_from_stdin():
  lines = []
  while True:
    line = input()
    if line == "":
      break
    lines.append(line)
  return lines

lines = read_lines_from_stdin()

for line in lines:
  print(line)
```

C++

```plaintext
#include <iostream>

int main() {
  std::string line;
  while (std::getline(std::cin, line)) {
    std::cout << line << std::endl;
  }
  return 0;
}
```

The Python code will read lines from stdin and print them to the console. The C++ code will also read lines from stdin and print them to the console. However, the Python code will be much faster than the C++ code.


---

Original Source: https://www.mindstick.com/forum/158991/why-is-reading-lines-from-stdin-much-slower-in-c-plus-plus-than-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
