When using Windows API (fileapi.h, ReadFile function) to load large files (about 2GB), the assuming time is unstable because of some mechanism of OS
////////////////// Use the following code to read the file and assign the variable “name” with your own file path ///////////////////////////
#include <windows.h>
#include <stdio.h>
#include <strsafe.h>
OVERLAPPED ol = { 0 };
int main(int argc, char** argv) {
HANDLE hFile;
char* ReadBuffer;
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
LARGE_INTEGER dwFileSize;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
// init name with the path for one large file (about 2GB or more) //
char name[128];
sprintf_s(name, 128, "path to your big file");
// use your oen file name
hFile = CreateFileA((char *)name,
GENERIC_READ, // open for writing
0, // do not share
NULL, // default security
OPEN_EXISTING, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Unable to open file %s, error code = %d.\n", name, GetLastError());
return 0;
}
int ret = GetFileSizeEx(hFile, &dwFileSize);
if (ret == 0)
{
printf("open file %d\n", dwFileSize.LowPart);
return 0;
}
ReadBuffer = (char*)GlobalAlloc(GMEM_FIXED, dwFileSize.LowPart);
if (ReadBuffer == NULL)
{
printf("globalalloc error\n");
return 0;
}
DWORD num;
if (FALSE == ReadFile(hFile, ReadBuffer, dwFileSize.LowPart, &num, &ol))
// if (FALSE == ReadFileEx(hFile, ReadBuffer, dwFileSize.LowPart, &ol, FileIOCompletionRoutine))
{
printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", GetLastError());
CloseHandle(hFile);
return 0;
}
//Decode_Model(ReadBuffer, dwFileSize.LowPart);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
printf("buffer: 0x08%x, size: %d, num: %d, error; %d, time elapse: %d us\n",
ReadBuffer, dwFileSize.LowPart, num, (dwFileSize.LowPart != num), ElapsedMicroseconds);
//fflush(stdout);
GlobalFree(ReadBuffer);
CloseHandle(hFile);
return 0;
}
/*
init char name[128]; with your own large file path
Compile this funtion to exe, and run for several times. The time for loading big files is unstable.
Could you explain whether this phenomenon is caused by some caching mechanism of Windows file reading API or some caching mechanism of the OS system.
What can I do to achieve stable file loading time?
Thank you.
*/