c的scanf为什么比c++的cin快
很早就知道,c的scanf(printf)比c++的快。刷题时尤其明显,在这上面超时是常有的事儿。
但,这是别人告诉我的,c快。
为什么快?
从网上借鉴一个例子做个简单测试:
1.cpp //向一个文件里输入1000000个随机数
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int num=;
int main()
{
ofstream file("data");
for(int i=;i<num;i++)
{
file<<rand()<<' ';
if((i+)%==)
file<<endl;
}
return ;
}
2.cpp //用cin读取这1000000个随机数
#include<iostream>
#include<ctime>
#include<cstdio>
#include<windows.h>
using namespace std;
const int num=;
int main()
{
freopen("data","r",stdin);
int i,n,start,end;
start=clock();
for(i=;i<num-;i++)
cin>>n;
end=clock();
cout<<double(end-start)/CLOCKS_PER_SEC<<endl;
Sleep();
system("pause");
return ;
}
结果: 耗时 5.281秒
3.cpp //用scanf读取这1000000个数
#include<ctime>
#include<cstdio>
#include<stdlib.h>
#include<windows.h>
#include<iostream>
using namespace std;
const int num=;
int main()
{
freopen("data","r",stdin);
int i,n,start,end;
start=clock();
for(i=;i<num;i++)
scanf("%d",&n);
end=clock();
//cout<<double(end-start)/CLOCKS_PER_SEC<<endl;
printf("%f\n",double(end-start)/CLOCKS_PER_SEC);
system("pause");
Sleep();
return ;
}
结果: 耗时 0.437秒
结论:scanf真的比cin快。竟快10倍。
运行环境,xp,DEV-C++。
比较合理的解释:默认情况,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输 出顺序错乱。正因为这个兼容性的特性,导致cin有许多额外的开销,如何禁用这个特性呢?只需一个语句 std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了,此时的cin就与scanf差不多 了。
另一种解释: cout在输出时总是要先将输出的存入缓存区。而printf直接调用系统进行IO,它是非缓存的。所以cout比printf慢。(这种解释,我没有验证)
c的scanf为什么比c++的cin快的更多相关文章
- cin 和scanf,scanf比cin快很多
//#include <iostream> #include <stdio.h> //#include <fstream> //using namespace st ...
- while(scanf("%d",&n)!=EOF) / while(cin>>n)终止问题
问题的发现:(想要看干货可以直接跳过这段) 我最近刚了解到关于栈的用法,于是按照参考书寻找代码,并把它敲到电脑上.编译运行代码后发现无法终止,在网上查找各种资料,总结如下. 因为我的电脑是Window ...
- cin快读
ios::sync_with_stdio(false); \\取消同步,cin,cout的速度就不慢了!!
- A - Space Elevator(动态规划专项)
A - Space Elevator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 【LeetCode】Roman to Integer(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- scanf和cin的差异
scanf和cin的差异 引例:http://www.cnblogs.com/shenben/p/5516996.html 大家都知道,在C++中有两种输入.输出方式—scanf和cin,但是,它们之 ...
- 关于scanf与cin哪个快的问题
一开始入c++的时候成天跑cin,cout 直到有一天用cin,cout超时 才知道scanf比cin快的多 但是后来又听说加了ios::sync_with_stdio(false);的cin跟飞一样 ...
- cin和scanf的速度差别
好长时间没有遇到这种问题了,以前虽然知道scanf比cin快,但是没想到快这么多,见图. 50万的数据. scanf输入: cin输入: 网上说用std::ios::sync_with_stdio(f ...
- Java中的“scanf()、cin()、input()"
最近在写一个Java程序时遇到一个问题,就是如何在Java里面输入数值,又叫做获取键盘输入值. 因为c语言里面有scanf(),C++里面有cin(),python里面有input().Java里面有 ...
随机推荐
- 九度OJ 1060:完数VS盈数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...
- Git you are not allowed to push code to protected branches on this project?
error: You are not allowed to push code to protected branches on this project....error: failed to pu ...
- 如何在MySQL中分配innodb_buffer_pool_size
如何在MySQL中分配innodb_buffer_pool_size innodb_buffer_pool_size是整个MySQL服务器最重要的变量. 1. 为什么需要innodb buffer p ...
- ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql 删除表时提示有外键 mysql> drop tables auth_group;ERROR 1217 (23000): Cannot delete or update a paren ...
- SecureCRT连接VMWare中Linux
SecureCRT连接vmvare虚拟机ubuntu的前提条件1. 使用主机可以ping通虚拟机,或虚拟机可以ping通主机2. 虚拟机已经开启ssh服务,可以使用sudo apt-get i ...
- 采集练习(十二) python 采集之 xbmc 酷狗电台插件
前段时间买了个树莓派才知道有xbmc这么强大的影音软件(后来我逐渐在 电脑.手机和机顶盒上安装xbmc),在树莓派上安装xbmc后树莓派就成为了机顶盒,后面在hdpfans论坛发现了jackyspy ...
- IOS UIlabel 、UIButton添加下划线
1.给UILabel 添加下划线 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )]; label.backgrou ...
- TCP服务器端和客户端程序设计【转】
本文转载自:http://blog.csdn.net/yueguanghaidao/article/details/7035248# 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.实验目的 ...
- HDU2243 考研路茫茫——单词情结 ——AC自动机、矩阵优化
题目链接:https://vjudge.net/problem/HDU-2243 考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memor ...
- Contiki 源码风格
/** * \defgroup coding-style Coding style * * This is how a Doxygen module is documented - start wit ...