2快,起码有10倍。在1中还有for的循环开销。

解决方案 »

  1.   

    tlovexyj(菠萝菠萝蜜)说得
    差不多
      

  2.   

    10倍是一个很公平的量。
    你可以这么比较,CFile::Read在第一个例子中执行了10次,这是主要的消耗,反而写1获10个bytes的那个代码到无足轻重了。
    另外告诉你一个事实,物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes.
    当然这中间有各种的cache机制,问题就复杂起来。
    但是,在大多数情况下,1是非常昂贵的做法
      

  3.   

    abentu() 说到我的心坎去了,非常感激。
      

  4.   

    abentu() 
    物理上写磁盘的最小单位是一个扇区或512bytes,就是说无论你写一个或10个bytes,都要读写磁盘512bytes照你这么说,如果要写的数据很多的话,那是一次写512bytes最好的了。
      

  5.   

    horsehorse(老马) 回复于2001-8-15 16:15:03   
    应该说只要内存允许,一次写入尽量多的数据。  请教一下,为什么这样说?
      

  6.   

    masterjames(james) 这个也是。zj_ok(zj_ok) 回复于2001-8-15 16:39:44   
    每次读写磁盘要经过寻道,磁盘定位的时间,两次写入512比一次写入1024要慢得多  对,说到了关键处。一定回分。
      

  7.   

    真不好意思啊!我做了一个测试程序,总共读书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.不信你试一下!
      

  8.   

    yyz_xyz(众众) 佩服你的对事态度。我想你的测试正好验证了,它们二者的不同。你看:
    第一种情况:
    for( i = 0;i<1000;i++)
                  f.Read(&c,1);第二种情况:
    for( i = 0;i<1000;i++)
                
              f.Read(buffer,1000);是否一样呢?
      

  9.   

    我说,你这个测试正好验证了,运行之一:
        for(int i = 0;i<10;i++)
            f.Read(&c,1);运行之二:
        f.Read(buffer,10);它们两者的速度。