Improving File Read Performance (single file, C++, Windows)
- by david
I have large (hundreds of MB or more) files that I need to read blocks from using C++ on Windows. Currently the relevant functions are:
errorType LargeFile::read( void* data_out, __int64 start_position, __int64 size_bytes ) const
{
if( !m_open ) {
// return error
}
else {
seekPosition( start_position );
DWORD bytes_read;
BOOL result = ReadFile( m_file, data_out, DWORD( size_bytes ), &bytes_read, NULL );
if( size_bytes != bytes_read || result != TRUE ) {
// return error
}
}
// return no error
}
void LargeFile::seekPosition( __int64 position ) const
{
LARGE_INTEGER target;
target.QuadPart = LONGLONG( position );
SetFilePointerEx( m_file, target, NULL, FILE_BEGIN );
}
The performance of the above does not seem to be very good. Reads are on 4K blocks of the file. Some reads are coherent, most are not.
A couple questions: Is there a good way to profile the reads? What things might improve the performance? For example, would sector-aligning the data be useful? I'm relatively new to file i/o optimization, so suggestions or pointers to articles/tutorials would be helpful.