你知道吗?运行的机理,CFile::Read()速度比较? 2快,起码有10倍。在1中还有for的循环开销。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 tlovexyj(菠萝菠萝蜜)说得差不多 10倍是一个很公平的量。你可以这么比较,CFile::Read在第一个例子中执行了10次,这是主要的消耗,反而写1获10个bytes的那个代码到无足轻重了。另外告诉你一个事实,物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes.当然这中间有各种的cache机制,问题就复杂起来。但是,在大多数情况下,1是非常昂贵的做法 abentu() 说到我的心坎去了,非常感激。 abentu() 物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes照你这么说,如果要写的数据很多的话,那是一次写512bytes最好的了。 horsehorse(老马) 回复于2001-8-15 16:15:03 应该说只要内存允许,一次写入尽量多的数据。 请教一下,为什么这样说? masterjames(james) 这个也是。zj_ok(zj_ok) 回复于2001-8-15 16:39:44 每次读写磁盘要经过寻道,磁盘定位的时间,两次写入512比一次写入1024要慢得多 对,说到了关键处。一定回分。 真不好意思啊!我做了一个测试程序,总共读书1000个字符,结果发现没什么差别!测试程序如下! CFile f("readme.txt",CFile::modeRead); char c; char buffer[1000]; int i; for( i = 0;i<10;i++) f.Read(&c,1); SYSTEMTIME SystemTime; CString strTime; GetLocalTime( (LPSYSTEMTIME) &SystemTime ); CTime t(SystemTime); strTime=t.Format("%Y-%m-%d %H:%M:%S"); cout<<"the begin time is "<<endl; cout<<LPCTSTR(strTime)<<endl; cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl; CString strcon1; for( i = 0;i<1000;i++) f.Read(&c,1); GetLocalTime( (LPSYSTEMTIME) &SystemTime ); CTime t2(SystemTime); strTime=t2.Format("%Y-%m-%d %H:%M:%S"); cout<<"the end time is "<<endl; cout<<LPCTSTR(strTime)<<endl; cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl; //cout<<LPCTSTR(strcon1)<<endl; cout<<"---------------------------"<<endl; GetLocalTime( (LPSYSTEMTIME) &SystemTime ); CTime t3(SystemTime); strTime=t3.Format("%Y-%m-%d %H:%M:%S"); cout<<"the begin time is "<<endl; cout<<LPCTSTR(strTime)<<endl; cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl; for( i = 0;i<1000;i++) f.Read(buffer,1000); GetLocalTime( (LPSYSTEMTIME) &SystemTime ); CTime t4(SystemTime); strTime=t4.Format("%Y-%m-%d %H:%M:%S"); cout<<"the end time is "<<endl; cout<<LPCTSTR(strTime)<<endl; cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl;结果发现在没什么差别,都是10ms.不信你试一下! yyz_xyz(众众) 佩服你的对事态度。我想你的测试正好验证了,它们二者的不同。你看:第一种情况:for( i = 0;i<1000;i++) f.Read(&c,1);第二种情况:for( i = 0;i<1000;i++) f.Read(buffer,1000);是否一样呢? 我说,你这个测试正好验证了,运行之一: for(int i = 0;i<10;i++) f.Read(&c,1);运行之二: f.Read(buffer,10);它们两者的速度。 关于一种限定的程序 急问!请问如何将自己的程序注册为Windows服务?在线等! 有关DECLARE_DYNCREATE宏的一个疑问 扫描端口,请求帮助! 怎样动态调整.rc中定义的控件的位置和大小? 如何设置connect()函数的超时时间? Xtreme ToolKit 4.1版本已经在10月8日出来了 一个菜菜的网络通讯问题,巨分 “多文档”Release 遇到一个棘手的问题 各位大侠帮帮忙 调试问题请高手指点! 请教学习方法的一类本质问题!
差不多
你可以这么比较,CFile::Read在第一个例子中执行了10次,这是主要的消耗,反而写1获10个bytes的那个代码到无足轻重了。
另外告诉你一个事实,物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes.
当然这中间有各种的cache机制,问题就复杂起来。
但是,在大多数情况下,1是非常昂贵的做法
物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes照你这么说,如果要写的数据很多的话,那是一次写512bytes最好的了。
应该说只要内存允许,一次写入尽量多的数据。 请教一下,为什么这样说?
每次读写磁盘要经过寻道,磁盘定位的时间,两次写入512比一次写入1024要慢得多 对,说到了关键处。一定回分。
CFile f("readme.txt",CFile::modeRead);
char c;
char buffer[1000];
int i;
for( i = 0;i<10;i++)
f.Read(&c,1);
SYSTEMTIME SystemTime;
CString strTime;
GetLocalTime( (LPSYSTEMTIME) &SystemTime );
CTime t(SystemTime);
strTime=t.Format("%Y-%m-%d %H:%M:%S");
cout<<"the begin time is "<<endl;
cout<<LPCTSTR(strTime)<<endl;
cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl;
CString strcon1;
for( i = 0;i<1000;i++)
f.Read(&c,1);
GetLocalTime( (LPSYSTEMTIME) &SystemTime );
CTime t2(SystemTime);
strTime=t2.Format("%Y-%m-%d %H:%M:%S");
cout<<"the end time is "<<endl;
cout<<LPCTSTR(strTime)<<endl;
cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl;
//cout<<LPCTSTR(strcon1)<<endl;
cout<<"---------------------------"<<endl; GetLocalTime( (LPSYSTEMTIME) &SystemTime );
CTime t3(SystemTime);
strTime=t3.Format("%Y-%m-%d %H:%M:%S");
cout<<"the begin time is "<<endl;
cout<<LPCTSTR(strTime)<<endl;
cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl;
for( i = 0;i<1000;i++)
f.Read(buffer,1000); GetLocalTime( (LPSYSTEMTIME) &SystemTime );
CTime t4(SystemTime);
strTime=t4.Format("%Y-%m-%d %H:%M:%S");
cout<<"the end time is "<<endl;
cout<<LPCTSTR(strTime)<<endl;
cout<<"milliseconds: "<<SystemTime.wMilliseconds<<endl;结果发现在没什么差别,都是10ms.不信你试一下!
第一种情况:
for( i = 0;i<1000;i++)
f.Read(&c,1);第二种情况:
for( i = 0;i<1000;i++)
f.Read(buffer,1000);是否一样呢?
for(int i = 0;i<10;i++)
f.Read(&c,1);运行之二:
f.Read(buffer,10);它们两者的速度。