cin,cout,printf,scanf效率对比
From:http://www.cnblogs.com/killerlegend/p/3918452.html
Author:KillerLegend
Date:2014.8.17
杭电OJ之3233很简单的一个问题,谁知道一直提示我超时,超时,最后都快哭了,C++代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int t,tmp,band,index=0;
while(cin>>t>>tmp>>band)
{
if(t==0||tmp==0||band==0) break;
double a,b,sum=0.0;
for(int i=0;i<t;++i)
{
cin>>a>>b;
sum+=a*(100-b)*0.01;
}
cout<<"Case "<<++index<<": "<<fixed<<setprecision(2)<<sum/band<<endl<<endl;
}
return 0;
}后来...将输入输出流换成了scanf和printf,结果就神奇的AC了...
#include <cstdio>
int main()
{
int t,tmp,band,index=0;
while(scanf("%d%d%d",&t,&tmp,&band))
{
if(t==0||tmp==0||band==0) break;
double a,b,sum=0.0;
for(int i=0;i<t;++i)
{
scanf("%lf%lf",&a,&b);
sum+=a*(100-b)*0.01;
}
printf("Case %d: %.2f\n\n",++index,sum/band);
}
return 0;
}这真是太让人伤心了...所以我打算测试一下cin和scanf,cout和printf:
cout测试:
#include <iostream>
#include <cstdio>
#include <time.h>
using namespace std;
int main()
{
freopen("in.conf","r",stdin);
freopen("out.conf","w",stdout);
clock_t t1,t2,t3,t4;
t1 = clock();
for(int i=0;i<10000000;++i)cout<<i<<" ";
t2 = clock();
cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
return 0;
}结果:5.206s
printf测试:
#include <iostream>
#include <cstdio>
#include <time.h>
using namespace std;
int main()
{
freopen("in.conf","r",stdin);
freopen("out.conf","w",stdout);
clock_t t1,t2,t3,t4;
t1 = clock();
for(int i=0;i<10000000;++i)printf("%d ",i);
t2 = clock();
cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
return 0;
}结果:6.202s
我们使用上面循环1千万次所产生的数据(78.89MB),用于下面读入时的测试数据:
使用cin读入:
#include <iostream>
#include <cstdio>
#include <time.h>
using namespace std;
int main()
{
freopen("out.conf","r",stdin);
freopen("in.conf","w",stdout);
clock_t t1,t2,t3,t4;
t1 = clock();
int k;
for(int i=0;i<10000000;++i)cin>>k;
t2 = clock();
cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
return 0;
}结果是38.507s
使用scanf读入:
#include <iostream>
#include <cstdio>
#include <time.h>
using namespace std;
int main()
{
freopen("out.conf","r",stdin);
freopen("in.conf","w",stdout);
clock_t t1,t2,t3,t4;
t1 = clock();
int k;
for(int i=0;i<10000000;++i)scanf("%d",&k);
t2 = clock();
cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
return 0;
}结果是4.204s
结果一览表:

结论:输出时尽量使用cout,输入时尽量使用scanf.
cin,cout,printf,scanf效率对比的更多相关文章
- cin/cout与scanf/printf的比较
转自http://www.cnblogs.com/penelope/articles/2426577.html cin .cout 基本说明: cin是标准输入流对象,代表标准输入设备(键盘), ...
- [笔记]cin、cout与scanf、printf的效率差异对比分析
之前上传UVa227 puzzle时,好不容易AC了,但发现自己用时50(ms),而在VJ上看到人家都是40ms.20ms,于是打开一个20ms的代码查看人家强在哪里.但结果研究了半天感觉差不多,于是 ...
- printf scanf cin cout的区别与特征
printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...
- 8-cin cout PK scanf printf(速度快慢问题对比)
我们在c++ 中使用cin cout很方便但速度很慢,导致有些题目用cin就超时而用scanf则就ac了,那到底改用谁? cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说 ...
- acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
题目 输入正好是long long的最大, 但是答案超long long 所以用unsigned, 不能用cin cout否则一定超时: 不能用abs(), abs 只用于整数. unsigned ...
- golang 浮点数 取精度的效率对比
需求 浮点数取2位精度输出 实现 代码 package main import ( "time" "log" "strconv" " ...
- read()/fread()/mmap()执行效率对比
一. read()/fread()/mmap()执行效率对比 系统调用read.c: #include <sys/types.h> #include <sys/stat.h> ...
- openCV 和GDI画线效率对比
一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...
- C++输入输出流 cin/cout 及格式化输出简介
C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...
随机推荐
- ElasticSearch查询 第三篇:词条查询
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
- 设计模式 笔记 代理模式 Proxy
//---------------------------15/04/21---------------------------- //Proxy 代理模式-----对象结构型模式 /* 1:意图: ...
- stl源码剖析 详细学习笔记priority_queue slist
// // priority_queue.cpp // 笔记 // // Created by fam on 15/3/16. // // //------------------------- ...
- winform 保存文件 打开文件 选择文件 字体样式颜色(流 using System.IO;)
string filePath = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) ...
- SpringBoot日记——Docker的使用
跟进互联网的浪潮有时候也挺难的,还没学完就出现新技术了…… 今天来说说,如何使用docker吧~ docker的安装配置 Docker是一个容器,我们怎么理解这个概念.我们做windows系统的时候会 ...
- OpenCV学习资源库
整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...
- Docker部署Registry私有镜像库
拉取镜像 docker pull registry:2.6.2 生成账号密码文件,这里采用htpasswd方式认证 docker run --rm --entrypoint htpasswd re ...
- Git版本库的创建(Ubuntu)
在Ubuntu上学习Git随笔. 一. git 仓库的安装 git 在终端用git命令查看Ubuntu是否安装git版本库,如果没有安装,最新版本(Ubuntu18.04)会提示用下面命令进行安装. ...
- Algorithms学习笔记-Chapter0序言
0.开篇 <Algorithms>源自Berkeley和UCSD课程讲义,由 Sanjoy Dasgupta / Christos H. Papadimitriou / Umesh V ...
- C++ 派生类成员的访问属性
派生类成员的访问属性: C++继承方式总共分为以下几种:public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则).(1)public(公有继承) ...