当我有一个大文件要读到内存时,如何最快呢?
下面的代码,对于BUFFSIZE的大小做了调整,并测试每次读取的时间,
int main(int argc, char *argv[])
{
unsigned int buffsize = 1024 * 512; //512k
int count = 0; //读取的字节数
int readcnt = 0; //读取次数
char* buff = new char[buffsize ];
QString filepath = "E:/tool/Qt/qt-enterprise-windows-x86-5.15.2.exe";
FILE *fd = fopen (filepath.toLatin1().data(), "rb");
QTime t1 = QTime::currentTime();
while (!feof (fd)){
count = fread (buff, sizeof (char), buffsize , fd);
readcnt++;
}
QTime t2 = QTime::currentTime();
int elapsed = t1.msecsTo(t2);
qDebug() <<"file size="<<count<<", readcnt="<< readcnt<< ", elapse is "<< elapsed <<
"ms";
fclose(fd);
}
测试读取的文件大小是2.35G,测试机器win10,
i5-9400 CPU @ 2.90GHz 2.90 GHz,32G内存
当BUFFSIZE 与读取时间的关系是:
4k --> 1670ms
8k --> 1077ms
16k --> 753ms
32k --> 591ms
64k --> 520ms
128k --> 496ms
256k --> 470ms
512k --> 441ms
1M --> 437ms
2M --> 442ms
4M ->504ms
16M --> 780ms
128M --> 742ms
512M --> 737ms
1G --> 753ms
4G --> 761ms
从数据上可以看出,分多次读取,并且每次读取512k或者1M时,时间最短。