测试了不同方法写文件的时间,测试环境是IBM T42:
代码一:
写1MB数据到磁盘,只计算文件操作部分的时间,执行时间是50毫秒。
代码二:
类似代码一,但是用的是 fstream类来写文件,执行时间是300毫秒。
对比了Java写文件所需要的时间,同环境Java写1MB数据的耗时是94毫秒,比使用fstream的速度快,比fwrite的方法慢。不过java的速度还是超过我的想象。
代码一:
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);
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);
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的速度还是超过我的想象。