Why is windows so slow in opening files first time and is there a faster way

 

Questions


This is Windows 7, 64 bit, Professional using

Consider a very simple loop

for (i = 0; i < names->size(); i++)
{
    std::string Name = names->at(i);
    HANDLE fileHandle = CreateFile(Name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);

    if (fileHandle == INVALID_HANDLE_VALUE)
    {
        throw "Failed";
    }

    CloseHandle(fileHandle);
}

If I run this on a directory with 863 files, it takes slightly over 22 SECONDS, or 25 milliseconds/file. The next time 169 milliseconds or 0.19 milliseconds/file. If I just use find_file time on a fresh directory is very fast, about the 0.2 ms/file. Although I used CreateFile here, other methods yield the same results.

Of course the answer is file caching: windows must be caching the information about file open. Moreover, it must be disk access as if the directory is on an SSD, the first and second opens are about the same.

Now the seek time on these hard drives are only 9 ms, so does anyone know WHAT IS WINDOWS doing when it takes 25 milliseconds to open/close a file for only reading with NO reading. But more importantly, is it possible to speed this up. You might say 25 ms is fast, but I am playing video files so I need to read at about 33 ms and an actual read of data is in the 30 ms range, so the cost of opening is too high.

Any suggestions on how to open a **load of files quickly would be appreciated.

 

 

————————————————-

Answer

Windows does a lot under the hood when you open a file. I’m no expert, but I can mention filesystem maintenance (updating the access time, journaling, etc.) and access permission checks as two important items. Unfortunately I don’t think you can make this any faster.

Of course if you don’t plan to read anything from the file, and you’re just checking if the file exists or reading some metadata like its modification time, there are faster ways to do that without opening the file.

c#,file-io,performance,windows

Facebook Comments

Post a comment