Home > Backend Development > C++ > Is `scanf()` in C Really Faster Than `cin` for Input?

Is `scanf()` in C Really Faster Than `cin` for Input?

Linda Hamilton
Release: 2024-11-23 10:38:11
Original
1022 people have browsed it

Is `scanf()` in C   Really Faster Than `cin` for Input?

Using scanf() in C : Is It Faster Than cin?

In C , it's widely believed that using cin and cout for input and output is significantly slower than using printf and scanf. This stems from the assumption that scanf() is optimized for C, while cin and cout are implemented using standard I/O functions that are slower.

Benchmark Results

To determine if this claim holds, a simple benchmark was conducted involving a program that reads a list of numbers from standard input and calculates their XOR. The program was implemented using both the iostream and scanf versions:

iostream Version:

#include <iostream>

int main() {
  int parity = 0;
  int x;

  while (std::cin >> x)
    parity ^= x;
  std::cout << parity << std::endl;

  return 0;
}
Copy after login

scanf Version:

#include <stdio.h>

int main() {
  int parity = 0;
  int x;

  while (1 == scanf("%d", &x))
    parity ^= x;
  printf("%d\n", parity);

  return 0;
}
Copy after login

When tested with a large dataset of over 33 million random numbers, the scanf version completed in 6.4 seconds, while the iostream version took a disappointing 24.3 seconds.

Optimization Impact

Compiler optimization settings did not significantly affect the results. This suggests that the speed difference is primarily due to inherent differences in the implementation of scanf() and cin/cout.

The Culprit: std::ios::sync_with_stdio

Further investigation revealed that the iostream I/O functions maintain synchronization with the C I/O functions. By default, this synchronization involves flushing the output buffer after each input operation, resulting in slower performance.

Disabling Synchronization

Fortunately, this synchronization can be disabled by calling std::ios::sync_with_stdio(false):

std::ios::sync_with_stdio(false);
Copy after login

After disabling synchronization, the performance of the iostream version improved dramatically, completing in a mere 5.5 seconds.

C iostream Wins

With synchronization disabled, C iostream becomes the faster option for this specific benchmark. It turns out that the internal syncing of std::cout is the primary culprit for iostream's slower performance.

Conclusion

While using scanf() can be faster in some cases, disabling std::ios::sync_with_stdio enables iostream to outperform scanf in many scenarios, especially when avoiding mixed usage of stdio and iostream functions.

The above is the detailed content of Is `scanf()` in C Really Faster Than `cin` for Input?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template