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效率对比的更多相关文章

  1. cin/cout与scanf/printf的比较

    转自http://www.cnblogs.com/penelope/articles/2426577.html  cin .cout   基本说明: cin是标准输入流对象,代表标准输入设备(键盘), ...

  2. [笔记]cin、cout与scanf、printf的效率差异对比分析

    之前上传UVa227 puzzle时,好不容易AC了,但发现自己用时50(ms),而在VJ上看到人家都是40ms.20ms,于是打开一个20ms的代码查看人家强在哪里.但结果研究了半天感觉差不多,于是 ...

  3. printf scanf cin cout的区别与特征

    printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...

  4. 8-cin cout PK scanf printf(速度快慢问题对比)

    我们在c++ 中使用cin cout很方便但速度很慢,导致有些题目用cin就超时而用scanf则就ac了,那到底改用谁? cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说 ...

  5. acdream B - 郭式树 (水题 卡cin,cout, 卡LL)

    题目 输入正好是long long的最大, 但是答案超long long 所以用unsigned, 不能用cin cout否则一定超时: 不能用abs(), abs 只用于整数. unsigned   ...

  6. golang 浮点数 取精度的效率对比

    需求 浮点数取2位精度输出 实现 代码 package main import ( "time" "log" "strconv" " ...

  7. read()/fread()/mmap()执行效率对比

    一. read()/fread()/mmap()执行效率对比 系统调用read.c: #include <sys/types.h> #include <sys/stat.h> ...

  8. openCV 和GDI画线效率对比

    一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...

  9. C++输入输出流 cin/cout 及格式化输出简介

    C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...

随机推荐

  1. BigDecimal工具类

    package config_service.slowcity; import java.math.BigDecimal; public class ConfigServerApp { /* * 小数 ...

  2. Java类加载器学习笔记

    今后一段时间会全面读一下<深入理解Java虚拟机> 在这里先记一下在网上看到的几篇介绍 类加载器 的文章,等读到虚拟机类加载机制再详细介绍. 超详细Java中的ClassLoader详解 ...

  3. LeetCode Letter Combinations of a Phone Number (DFS)

    题意 Given a digit string, return all possible letter combinations that the number could represent. A ...

  4. Centos 7 安装mysql5.7.24二进制 版本

    Mysql 二进制安装方法 下载mysql https://dev.mysql.com/downloads/mysql/ 1.解压包 tar xf mysql-5.7.24-linux-glibc2. ...

  5. zabbix3.4 实现sendEmail邮件报警

    zabbix3.4实现sendEmail邮件报警 转发:https://www.cnblogs.com/pythonal/p/7813948.html sendEmail是一个轻量级,命令行的SMTP ...

  6. LeetCode 9. Palindrome Number(回文数)

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  7. PTA (Advanced Level) 1001 A+B Format

    1001 A+B Format Calculate a+b and output the sum in standard format -- that is, the digits must be s ...

  8. 升级framework4.0后form认证票据失效的问题

    好久没来了,密码都差点忘了,顺便记录下今天配置环境碰到的小问题 网站使用的form authentication做SSO登录,登录域名使用的framework20配置环境 一个栏目升级为4.0环境后, ...

  9. dubbo底层之Netty

    背景 Java线程:由开始的单线程,到通过new Thread()创建的多线程,再到现如今的线程池,Java多线程编程的效率和性能有了很大的提升 Reactor模型:基于事件驱动,适合处理海量I/O事 ...

  10. CopyOnWriteArrayList、CopyOnWriteArraySet、ConcurrentHashMap的实现原理和适用场景

    ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储的,同步Map在同步的 ...