QT - QFile -从文件读取缓慢
QT - QFile - slow read from file
我想计算文件的MD5哈希值。
如果我使用fast_md5与本地库http://twmacinta.com/myjava/fast_md5.php,那么Java读取文件从硬盘90 MB/s…120MB/s,计算时间70秒。
如果使用QT
QCryptographicHash hash(QCryptographicHash::Md5);
QFile in("C:/file.mkv");
QFileInfo fileInfo("C:/file.mkv");
qint64 imageSize = fileInfo.size();
const int bufferSize = 1000000;
char buf[bufferSize+5];
int bytesRead;
if (in.open(QIODevice::ReadOnly)) {
while ( (bytesRead = in.read(buf, bufferSize)) > 0) {
// imageSize -= bytesRead;
// hash.addData(buf, bytesRead);
}
}
else {
qDebug() << "Failed to open device!";
}
in.close();
qDebug() << hash.result().toHex();
然后我的程序以20…78的速度从硬盘读取文件MB/s,计算时间为210秒。
是否有可能在QT中加速MD5 Calc的处理?可能需要将缓冲区从1000000增加到更大的值?
最佳解决方案是
/*max bytes to read at once*/
static const qint64 CHUNK_SIZE = 1024;
/*init hash*/
QCryptographicHash hash(Sha1);
/*open file*/
QFile file("foo.bar");
if (!file.open(QIODevice::ReadOnly))
return;
/*process file contents*/
QByteArray temp = file.read(CHUNK_SIZE);
while(!temp.isEmpty())
{
hash.addData(temp);
temp = file.read(CHUNK_SIZE);
}
/*finalize*/
const QByteArray res = hash.result();
qDebug("Hash is: %s", res.toHex());
这个解决方案为我提供了最好的读取速度(大约100- 120mb/s)和最好的计算时间- 51…75秒。
从https://qt-project.org/forums/viewthread/41886相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 使用istringstearm和get行缓慢读取文件
- 缓慢的文件读取和复制到内存-C++
- QT - QFile -从文件读取缓慢
- c++编写和读取文本文件是非常缓慢的,任何替代方案