PDA

View Full Version : Performance problem with QFile.



Abdeljalil
26th April 2014, 18:06
I’m trying to calculate the hash of some disk image files (.iso)
so i have to read the entire file and store it in QByteArray to pass it later to QCryptographicHash::Hash
the problem is with reading performance, it takes a lot of time and the system freeze while using QFile::readAll() and storing it in byte array.
is there any solution to reduce the time and prevent the system form freezing?

ecanela
26th April 2014, 18:25
use the QCryptographicHash::addData(), instead QCryptographicHash::hash(),



#include <QCryptographicHash>
#include <QFile>
#include <QDebug>

int main()
{
QCryptographicHash crypto(QCryptographicHash::Md5);
QFile file("linuxmint-16-xfce-dvd-64bit.iso");

if ( !file.open(QFile::ReadOnly) ){
qDebug() << "ERROR: cant read file";
}

qint64 size = 4 * 1024 * 1024; // segment of 4MB
int iter = 0;
while(!file.atEnd()){
crypto.addData(file.read(size));

++iter;
qDebug() << "iteration #" << iter
<< " data offset; " << size*iter << "(bytes)" ;
}

QByteArray hash = crypto.result();
qDebug() << hash.toHex();
return true;
}



i test this minimal example, only replace the filepath