写文件速度测试

测试了不同方法写文件的时间,测试环境是IBM T42:

代码一:
    char *data=new char[1024*1024];
    
int t0 = GetTickCount();

    FILE 
*pFile = NULL;
    
    pFile 
= fopen("D:/test1MB_C.dat""a+");

    fwrite(data, 
1024*1024,1,pFile);
    fclose(pFile);

    
int t1 = GetTickCount();

    delete[] data;
    data 
= NULL;

    
char szText[32];
    sprintf(szText, 
"use %d second(%d 毫秒).", (t1-t0)/1000, t1-t0 );
    
this->SetWindowText(szText);

写1MB数据到磁盘,只计算文件操作部分的时间,执行时间是50毫秒。

代码二:
    char *data=new char[1024*1024];

    
int t0 = GetTickCount();
    std::fstream fsFile(
"D:/test1MB.dat" , std::ios::out | std::ios::binary);
    
    fsFile.write( data,  
1024*1024 ); 
    
    fsFile.close();

    
int t1 = GetTickCount();

    delete[] data;
    data 
= NULL;

    
char szText[32];
    sprintf(szText, 
"use %d second(%d 毫秒).", (t1-t0)/1000, t1-t0 );
    
this->SetWindowText(szText);


类似代码一,但是用的是fstream类来写文件,执行时间是300毫秒。

对比了Java写文件所需要的时间,同环境Java写1MB数据的耗时是94毫秒,比使用fstream的速度快,比fwrite的方法慢。不过java的速度还是超过我的想象。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值