在 C++ 程序中使用 scanf() 比使用 cin 快吗? [英] Using scanf() in C++ programs is faster than using cin?

查看:26
本文介绍了在 C++ 程序中使用 scanf() 比使用 cin 快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否属实,但是当我阅读有关提供问题的网站之一的常见问题解答时,我发现了一些引起我注意的东西:

I don't know if this is true, but when I was reading FAQ on one of the problem providing sites, I found something, that poke my attention:

检查您的输入/输出方法.在 C++ 中,使用 cin 和 cout 太慢了.使用这些,您将保证无法通过大量输入或输出解决任何问题.改用 printf 和 scanf.

Check your input/output methods. In C++, using cin and cout is too slow. Use these, and you will guarantee not being able to solve any problem with a decent amount of input or output. Use printf and scanf instead.

有人可以澄清一下吗?在 C++ 程序中使用 scanf() 真的比使用 cin >> something 更快吗?如果是,在 C++ 程序中使用它是一个好习惯吗?我认为它是特定于 C 的,虽然我只是在学习 C++...

Can someone please clarify this? Is really using scanf() in C++ programs faster than using cin >> something ? If yes, that is it a good practice to use it in C++ programs? I thought that it was C specific, though I am just learning C++...

推荐答案

这是一个简单案例的快速测试:一个从标准输入读取数字列表并对所有数字进行异或的程序.

Here's a quick test of a simple case: a program to read a list of numbers from standard input and XOR all of the numbers.

iostream 版本:

#include <iostream>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

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

  return 0;
}

scanf 版本:

#include <stdio.h>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

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

  return 0;
}

结果

使用第三个程序,我生成了一个包含 33,280,276 个随机数的文本文件.执行时间为:

Using a third program, I generated a text file containing 33,280,276 random numbers. The execution times are:

iostream version:  24.3 seconds
scanf version:      6.4 seconds

改变编译器的优化设置似乎根本没有改变结果.

Changing the compiler's optimization settings didn't seem to change the results much at all.

因此:确实存在速度差异.

Thus: there really is a speed difference.

用户 clyfish 在下面指出速度差异主要是由于iostream I/O 函数与 CI/O 函数保持同步.我们可以通过调用 std::ios::sync_with_stdio(false);:

User clyfish points out below that the speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false);:

#include <iostream>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

  std::ios::sync_with_stdio(false);

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

  return 0;
}

新结果:

iostream version:                       21.9 seconds
scanf version:                           6.8 seconds
iostream with sync_with_stdio(false):    5.5 seconds

C++ iostream 胜出! 事实证明,这种内部同步/刷新通常会减慢 iostream i/o.如果我们不混合使用 stdio 和 iostream,我们可以将其关闭,然后 iostream 是最快的.

C++ iostream wins! It turns out that this internal syncing / flushing is what normally slows down iostream i/o. If we're not mixing stdio and iostream, we can turn it off, and then iostream is fastest.

代码:https://gist.github.com/3845568

这篇关于在 C++ 程序中使用 scanf() 比使用 cin 快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆