C++的文件操作速度比C的文件操作速度低那么多呀?

htty326 2008-08-29 11:02:54
今天做了一个测试,分别用C++中fstream库中的类和C语言中的FILE来复制一个10M大小的文件,结果比较令人感到意外。
C++代码如下:
#include <fstream>
#include <windows.h>
#include <iostream>

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

std::ofstream ofile;
std::ifstream ifile;
int BuSize=1024*1024*2;
int t1=0,t2=0;
char* Buffer=new char[BuSize];

ifile.open("D:\\Data.txt",std::ios::in);
ofile.open("D:\\Data2.txt",std::ios::out);

t1=::GetTickCount();
while(!ifile.eof())
{
ifile.read(Buffer,BuSize);
ofile.write(Buffer,ifile.gcount());
}
t2=::GetTickCount();
std::cout < <t2-t1 < <std::endl;
ifile.close();
ofile.close();
return 0;
}
运行后,时间为4727毫秒

C语言代码如下:
#include <stdio.h>
#include <iostream>
#include <windows.h>

int main(int argc, char* argv[])
{
FILE *fhr=NULL,*fhw=NULL;
int BuSize=1024*1024*2,DataSize=0;
int t1=0,t2=0;
char* Buffer=new char[BuSize];

fhr=fopen("D:\\Data.txt","r");
fhw=fopen("D:\\Data1.txt","w");
t1=::GetTickCount();

while(!feof(fhr))
{
DataSize=fread(Buffer,sizeof(char),BuSize,fhr);
fwrite(Buffer,sizeof(char),DataSize,fhw);
}

t2=::GetTickCount();
std::cout < <t2-t1 < <std::endl;
fclose(fhr);
fclose(fhw);
return 0;
}
运行后,时间为234毫秒

C语言代码的执行时间比C++代码的执行时间快了20倍,难道C++的文件操作效率就那么低嘛?
我一直以来都以为C++会比C的速度慢不了多少,不过今天的测试真是有点出乎意料了……
...全文
707 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmsuccess 2008-09-04
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <windows.h>
#include <iostream>
using namespace std;

int main() {

const int BufSize = 1024 * 1024 * 2;
char* buffer = new char[BufSize];

for (int i = 0; i < 10; ++i)
{
cout<<"test "<<i<<endl;

DWORD s = GetTickCount();

ifstream ifile1("test.dat", ios::binary);
ofstream ofile1("cpp.dat", ios::binary);

while (true)
{
ifile1.read(buffer, BufSize);
ofile1.write(buffer, ifile1.gcount());

if (ifile1.eof())
break;
}

ifile1.close();
ofile1.close();

cout<<"cpp time : "<<GetTickCount() - s<<endl;


s = GetTickCount();

FILE* ifile2 = fopen("test.dat", "rb");
FILE* ofile2 = fopen("c.dat", "wb");
fseek (ifile2, 0L, SEEK_END);
size_t lSize =ftell(ifile2);
rewind(ifile2);
char * buffer1;
buffer1 = (char*) malloc (sizeof(char)*lSize);
if (buffer1 == NULL)
{
fputs ("Memory error",stderr);
exit (2);
}
long result = fread (buffer1,sizeof(char),lSize,ifile2);
if (result != lSize)
{
fputs ("Reading error",stderr);
exit (3);
}
fwrite(buffer1, sizeof(char),lSize, ofile2);
fclose(ifile2);
fclose(ofile2);

cout<<"c time : "<<GetTickCount() - s<<endl;
cout<<endl;
}

delete[] buffer;


return 0;
}


我这儿c就快很多

结果:
test 0
cpp time : 26938
c time : 1484

test 1
cpp time : 29282
c time : 2719

test 2
cpp time : 29156
c time : 1610

test 3
cpp time : 28437
c time : 938

test 4
cpp time : 28500
c time : 937

test 5
cpp time : 28375
c time : 2172

test 6
cpp time : 27781
c time : 2078

test 7
cpp time : 28609
c time : 2625

test 8
cpp time : 31516
c time : 1625

test 9
cpp time : 28016
c time : 3672

Press any key to continue
zhang_zhen12 2008-09-04
  • 打赏
  • 举报
回复
我看书的时候说C++比c语言差5%左右 java应该比C++差点吧
星羽 2008-09-04
  • 打赏
  • 举报
回复

#include "iostream"
#include "stdlib.h"
#include "fstream"
#include "windows.h"
using namespace std;

int main() {

const int BufSize = 1024 * 1024 * 2;
char* buffer = new char[BufSize];

for (int i = 0; i < 10; ++i)
{
cout<<"test "<<i<<endl;

DWORD s = GetTickCount();

ifstream ifile1("test.dat", ios::binary);
ofstream ofile1("cpp.dat", ios::binary);

while (true)
{
ifile1.read(buffer, BufSize);
ofile1.write(buffer, ifile1.gcount());

if (ifile1.eof())
break;
}

ifile1.close();
ofile1.close();

cout<<"cpp time : "<<GetTickCount() - s<<endl;


s = GetTickCount();

FILE* ifile2 = fopen("test.dat", "rb");
FILE* ofile2 = fopen("c.dat", "wb");

while (true)
{
size_t size = fread(buffer, 1, BufSize, ifile2);
fwrite(buffer, size, 1, ofile2);

if (feof(ifile2))
break;
}

fclose(ifile2);
fclose(ofile2);

cout<<"c time : "<<GetTickCount() - s<<endl;
cout<<endl;
}

delete[] buffer;


return 0;
}

测试文件 35.0 MB (36,758,319 字节)

结果

test 0
cpp time : 2109
c time : 719

test 1
cpp time : 734
c time : 1031

test 2
cpp time : 2219
c time : 953

test 3
cpp time : 813
c time : 1203

test 4
cpp time : 953
c time : 515

test 5
cpp time : 579
c time : 890

test 6
cpp time : 1235
c time : 1468

test 7
cpp time : 782
c time : 671

test 8
cpp time : 563
c time : 875

test 9
cpp time : 1344
c time : 1359

请按任意键继续. . .
blackcat242 2008-09-04
  • 打赏
  • 举报
回复
C++中的字符流在不断的分配内存释放内存,这个比较耗时的。就好像你用string和一个char*分别来处理变长的字符串,效率会差很多。以前用string处理一个日志文件,要1个多小时才能解析完,但是换成char*来处理,1s,so fast
htty326 2008-08-30
  • 打赏
  • 举报
回复
呵呵……我的程序是debug版的,我在G++和vs2005下测试也是用的debug版的,在每个编译器下我都运行了五六次,然后取了个平均值……
caimps 2008-08-30
  • 打赏
  • 举报
回复
作比较要尽可能在相同条件下
tangshuiling 2008-08-30
  • 打赏
  • 举报
回复
mark
Super.Jiju 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 htty326 的帖子:]
今天做了一个测试,分别用C++中fstream库中的类和C语言中的FILE来复制一个10M大小的文件,结果比较令人感到意外。
C++代码如下:
#include <fstream>
#include <windows.h>
#include <iostream>

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

std::ofstream ofile;
std::ifstream ifile;
int BuSize=1024*1024*2;
int t1=0,t2=0;
char* Buffer=new char[BuSize];

ifile.open("D:\\Data.txt",std::ios::in);
ofile.o…
[/Quote]

都有各自的优点

据说在C++中关键部分用汇编写,提高的速度是相当可观的
two_ears 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jieao111 的回复:]
1,c++一般用得比c安全,既然速度在可接受范围内的话,我选择更安全的

2,你的这个测试有点偶然性。你何不多做些程序,统计数据来说明问题。只靠文件操作一个程序来说c++比c慢20倍,有点说不过去吧。我记得看过各个语言效率的统计表,c++应该和Java差不多,都比c慢了几倍吧,大约是5倍一下
[/Quote]

C++比java还是快很多吧?和完成什么工作也应该有关系
zonghenglls 2008-08-29
  • 打赏
  • 举报
回复
在我的机器上,差了30多倍
jieao111 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 htty326 的回复:]
不过比较奇怪的是在执行复制操作的时候,硬盘灯不怎么闪,倒是CPU的平均使用率达到了60%以上,我的CPU是T7250的……
[/Quote]
这个我就不知道了,不过优化时记得“20%的代码占用了80%的时间”,把那个瓶颈搞定就行了。
htty326 2008-08-29
  • 打赏
  • 举报
回复
不过比较奇怪的是在执行复制操作的时候,硬盘灯不怎么闪,倒是CPU的平均使用率达到了60%以上,我的CPU是T7250的……
htty326 2008-08-29
  • 打赏
  • 举报
回复
呵呵……最近在写一个数据库文件,在执行数据插入的时候,采用的是临时文件替换的方法,当数据量大于10M的时候,发现其速度明显地慢了下来,到20M的时候就有点无法忍受了……程序半天没有响应,后来检查了一下程序,原来是在把源文件内容复制到临时文件的那段代码耗费了大量的时间,于是今天才做了这个例子来说明……
walter2001 2008-08-29
  • 打赏
  • 举报
回复
这个效率对比确实有点意外,有的说C++和Java差不多也很让人意外。

呼唤达人贴出权威的C、C++、Java的执行效率对比表,^_^
zjw6861982 2008-08-29
  • 打赏
  • 举报
回复
语言的使用要看场合的。合适度,效率、易用性都要考虑。
jieao111 2008-08-29
  • 打赏
  • 举报
回复
1,c++一般用得比c安全,既然速度在可接受范围内的话,我选择更安全的

2,你的这个测试有点偶然性。你何不多做些程序,统计数据来说明问题。只靠文件操作一个程序来说c++比c慢20倍,有点说不过去吧。我记得看过各个语言效率的统计表,c++应该和Java差不多,都比c慢了几倍吧,大约是5倍一下
wuyu637 2008-08-29
  • 打赏
  • 举报
回复
buffer不同
fstream的默认buffer比较小,所以要不断的重复调用,函数的重复调用就造成了很大的开销,
mLee79 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 Oversense 的回复:]
大哥,你是debug版吧
[/Quote]
cjh3382700 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 ablenavy 的回复:]
哦?!
看来VC++ 6.0该退出了
[/Quote]
richbirdandy 2008-08-29
  • 打赏
  • 举报
回复
恩 顶15楼 lz的实验从各种因素上看都不具说服力
加载更多回复(10)
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧