1

I am writing C++ code to enumerate whole HDD and drive listing, however it takes more than 15 minutes to complete the disk enumeration of all drives (HDD capacity 500GB). and compile the response in Binary file.

However, I have a 3rd party Executable which gives me the listing of whole Disk in just less than two minutes ... Can you please look into my code and suggest me some performance improvement techniques.

EnumFiles(CString FolderPath, CString SearchParameter,WIN32_FIND_DATAW *FileInfoData)
{
         CString SearchFile = FolderPath + SearchParameter;
         CString FileName;

        hFile = FindFirstFileW(SearchFile, FileInfoData); //   \\?\C:\*

            if (hFile == INVALID_HANDLE_VALUE)
            {
                // Error
            }
          else
         {
                do
                {
                    FileName = FileInfoData->cFileName; 
                    if (FileInfoData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                    {
                        if (! (FileName == L"." || FileName == L".."))
                        {
                                        // Save the Folder Information
                          EnumFiles(FolderPath + FileName +(L"\\"), SearchParameter,FileInfoData);
                        }
                    }
                    else
                   {
                     // Save the File Parameters 
                    }     

              } while (FindNextFileW(hFile, FileInfoData));
}
           FindClose(hFile);
       }
  • 1
    Possible optimization for NTFS volumes: Read the raw NTFS tables instead of using the Windows API. The same optimization can be adapted to other file systems. – IInspectable Jan 31 '14 at 16:07
  • Another optimization is to not call `EnumFiles()` inside the loop, thus having multiple search handles open at one time. Gather the subfolders into a temporary list first, then close the search handle and run a second loop to call `EnumFiles()`. – Remy Lebeau Jan 31 '14 at 16:39
  • In line with @RemyLebeau's suggestion, you might want to look at [code I posted](http://stackoverflow.com/a/9103533/179910) some time ago answering a similar question. Note that while this *will* usually improve performance, an improvement from 15 to 2 minutes would be...unprecedented, at least in my experience. – Jerry Coffin Feb 01 '14 at 04:05
  • It is likely that the application in question scans the master file table rather than iterating through the directory tree. See this answer: http://stackoverflow.com/a/7459109/886887 – Harry Johnston Feb 04 '14 at 03:06

0 Answers0