Chinaunix首页 | 论坛 | 博客
  • 博客访问: 288907
  • 博文数量: 72
  • 博客积分: 2387
  • 博客等级: 大尉
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:54
文章分类

全部博文(72)

文章存档

2012年(1)

2011年(1)

2010年(70)

分类: C/C++

2010-08-26 12:41:03

TCTimeCost timea;
        timea.Start();
       
        char buf[1024*10];
        int iFd = open("data.txt",O_RDONLY);
        if(iFd == -1){
                cout<<"open file:"<<"data.txt"<<" error!!!"<                return -1;
        }
        int isize = 0;
        int pos,pos2;
        string slastrec,srec;
        while((isize = read(iFd,buf,8192))>0){
                //cout<<"slastrec.size="<                //cout<<"slastrec="<                if(slastrec.size()>0)
                        srec = slastrec + string(buf);
                else
                        srec = buf;
                //cout<<"srec="<                pos = pos2 = 0 ;
                slastrec = "";
                while((pos=srec.find("\n", pos))!= string::npos){
                  //cout<<"jf rec="<                  pos2 = ++pos;
                }
                int recsize = srec.size();
                if(pos2                        slastrec = srec.substr(pos2,recsize-pos2);
               
        }
        close(iFd);
       
        cout<<"read file time:"<        timea.Start();
        ifstream  iFile("data.txt");
        while(iFile.good()){
                iFile.getline(buf, sizeof(buf));
                //cout<<"rec="<        }
        iFile.close();
        cout<<"fstream file time:"<

 

测试结果,源文件条数:1741条,可以看到用块读比一行一行读快60倍
read file time:10516
fstream file time:657857

阅读(2076) | 评论(0) | 转发(0) |
0

上一篇:strlen

下一篇:函数指针

给主人留下些什么吧!~~