PDA

View Full Version : QFile map



weixj2003ld
21st February 2011, 07:54
Now I need to read/write a large file(15G),I want to accelerate the speed of reading /writing file, I find that the map function of QFile can do it,I try it as follows,but I find that speed is the same as the common read/write file,why?


QFile objInputFile("test.sgy");
if (!objInputFile.open(QIODevice::Unbuffered | QIODevice::ReadOnly))
{
return false;
}

QFile objOutputFile("out.sgy");
if (!objOutputFile.open(QIODevice::Unbuffered | QIODevice::WriteOnly | QIODevice::Truncate))
{
objInputFile.close();
return false;
}

uchar * pData = NULL;
qint64 m_qint64FileOffset = 0;
qint64 m_qint64InputFileSize = objInputFile.size();
qint64 m_unBlockSize=m_qint64InputFileSize;

qint64 unTmpBlockSize = m_qint64InputFileSize/10;

while(m_qint64FileOffset < m_qint64InputFileSize)
{
if (m_qint64InputFileSize-m_qint64FileOffset <unTmpBlockSize)
unTmpBlockSize=m_qint64InputFileSize-m_qint64FileOffset;
pData = objInputFile.map(m_qint64FileOffset,unTmpBlockSize );

if (pData == NULL)
{
objInputFile.close();
objOutputFile.close();
return false;
}
if (objOutputFile.write((char*)pData,unTmpBlockSize) == -1)
{
objInputFile.close();
objOutputFile.close();
return false;
}

m_qint64FileOffset += unTmpBlockSize;
if (!objInputFile.unmap(pData))
{
objInputFile.close();
objOutputFile.close();
return false;
}
}

objInputFile.unmap(pData);
objInputFile.close();
objOutputFile.close();
return true;

squidge
21st February 2011, 09:17
For large files, the speed will be the same regardless of the method you use because the overall performance will be based on the OS/disk controller and disks themselves rather than the overhead of the libraries you are using.