《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化

《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化,第1张

概述Delphi 版 everything、光速搜索代码》,文章中关于获取文件全路径函数:GetFullFileName,有一个地方值得优化。 就是有多个文件,它们可能属于同一个目录。 譬如 System32 目录下有2000多个文件,GetFullFileName 还是进行了2000多次的查询,效率肯定是受影响的。 先处理目录,获取目录全路径名称。 然后文件只用查询一次,就知道它的父路径的全路径了

Delphi 版 everything、光速搜索代码》,文章中关于获取文件全路径的函数:GetFullfilename,有一个地方值得优化。

就是有多个文件,它们可能属于同一个目录。

譬如 System32 目录下有2000多个文件,GetFullfilename 还是进行了2000多次的查询,效率肯定是受影响的。

先处理目录,获取目录全路径名称。

然后文件只用查询一次,就知道它的父路径的全路径了。效率肯定会提高的。尝试了一下。

[delphi]  view plain  copy   { 获取文件全路径,包含路径和文件名 }   procedure GetFullfilename(var fileList: TStringList; const chrLogicldiskname: Char; const bSort: Boolean = False);   var     UInt64DirList    : TArray<UInt64>;     III              : Integer;     UPID             : UInt64;     intIndex         : Integer;     dirList          : TStringList;     intDirectoryCount: Integer;   begin     { 将 fileList 按 fileReferenceNumber 数值排序 }     fileList.sorted := False;     fileList.CustomSort(Int64Sort);        { 先处理目录,获取路径的全路径名称 }     dirList := TStringList.Create;     try       { 获取目录的总数 }       intDirectoryCount := 0;       for III           := to fileList.Count - do       begin         if PfileInfo(fileList.Objects[III])^.bDirectory then         begin           Inc(intDirectoryCount);         end;       end;       SetLength(UInt64DirList, intDirectoryCount);          { 将所有目录信息添加到目录列表 }       intDirectoryCount := 0;       for III           := to fileList.Count - do       begin         if PfileInfo(fileList.Objects[III])^.bDirectory then         begin           dirList.Addobject(PfileInfo(fileList.Objects[III])^.strfilename, fileList.Objects[III]);           UInt64DirList[intDirectoryCount] := PfileInfo(fileList.Objects[III])^.fileReferenceNumber;           Inc(intDirectoryCount);         end;       end;          { 获取目录的全路径名称 }       intDirectoryCount := 0;       for III           := to fileList.Count - do       begin         if PfileInfo(fileList.Objects[III])^.bDirectory then         begin           UPID := PfileInfo(fileList.Objects[III])^.ParentfileReferenceNumber;           while TArray.BinarySearch(UInt64DirList, UPID, intIndex) do           begin             UPID                  := PfileInfo(dirList.Objects[intIndex])^.ParentfileReferenceNumber;             fileList.Strings[III] := PfileInfo(dirList.Objects[intIndex])^.strfilename + ‘\‘ + fileList.Strings[III];           end;           fileList.Strings[III]              := (chrLogicldiskname + ‘:\‘ + fileList.Strings[III]);           dirList.Strings[intDirectoryCount] := fileList.Strings[III];           Inc(intDirectoryCount);         end;       end;          { 再获取每个文件的全路径 }       for III := to fileList.Count - do       begin         if not PfileInfo(fileList.Objects[III])^.bDirectory then         begin           UPID := PfileInfo(fileList.Objects[III])^.ParentfileReferenceNumber;           if TArray.BinarySearch(UInt64DirList, intIndex) then           begin             fileList.Strings[III] := dirList.Strings[intIndex] + ‘\‘ + fileList.Strings[III];           end           else           begin             fileList.Strings[III] := chrLogicldiskname + ‘\‘ + fileList.Strings[III];           end;         end;       end;          { 将所有文件按文件名排序 }       if bSort then         fileList.sort;     finally       dirList.Free;     end;   end;  

这个函数比原来的函数效率上刚好提高了一倍。

100万个的文件,耗时4秒左右。200万个的文件,耗时8秒左右。

 

 

注:原有的  TfileInfo 添加个目录属性:

  TfileInfo = record
    strfilename: String;               // 文件名称
    bDirectory: Boolean;               // 是否是目录 <增加>
    fileReferenceNumber: UInt64;       // 文件的ID
    ParentfileReferenceNumber: UInt64; // 文件的父ID

  end;

在代码 

fileList.Addobject(strfilename,TObject(pfi)); 

前,添加一行:

pfi^.bDirectory  := UsnRecord^.fileAttributes and file_ATTRIBUTE_DIRECTORY = file_ATTRIBUTE_DIRECTORY;

总结

以上是内存溢出为你收集整理的《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化全部内容,希望文章能够帮你解决《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1274459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存