简体   繁体   English

Delphi 对于大小 > 10gb 的文件大小最快

[英]Delphi fastest FileSize for sizes > 10gb

Wanted to check with you experts if there are any drawbacks in this funtion.如果此功能有任何缺点,想咨询您的专家。 Will it work properly on the various Windows OS?它会在各种 Windows 操作系统上正常工作吗? I am using Delphi Seattle (32 and 64 bit exe's).我正在使用 Delphi 西雅图(32 位和 64 位 exe)。 I am using this instead of Findfirst for its speed.我使用它而不是 Findfirst 来提高速度。

function GetFileDetailsFromAttr(pFileName:WideString):int64;
var
  wfad: TWin32FileAttributeData;
  wSize:LARGE_INTEGER ;
begin
  Result:=0 ;
  if not GetFileAttributesEx(pwidechar(pFileName), GetFileExInfoStandard,@wfad) then
    exit;

  wSize.HighPart:=wfad.nFileSizeHigh ;
  wSize.LowPart:=wfad.nFileSizeLow  ;
  result:=wsize.QuadPart ;
end;

The typical googled samples shown with this command does not work for filesize > 9GB使用此命令显示的典型 google 示例不适用于文件大小 > 9GB

function GetFileAttributesEx():Int64 using 
begin
...
  result:=((&wfad.nFileSizeHigh) or (&wfad.nFileSizeLow))

Code with variant record is correct.带有变体记录的代码是正确的。

But this code但是这段代码

result:=((&wfad.nFileSizeHigh) or (&wfad.nFileSizeLow))

is just wrong, result cannot overcome 32-bit border是错的,结果无法克服 32 位边界

Code from link in comment评论中链接的代码

result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);

is wrong because it does not account how compiler works with 32 and 64-bit values.是错误的,因为它没有考虑编译器如何处理 32 位和 64 位值。 Look at the next example showing how to treat this situation properly (for value d, e):看下一个例子,展示如何正确处理这种情况(对于值 d,e):

var
  a, b: DWord;
  c, d, e: Int64;
  wSize:LARGE_INTEGER ;
begin
  a := 1;
  b := 1;
  c := Int64(a) or Int64(b shl 32);
  d := Int64(a) or Int64(b) shl 32;
  wSize.LowPart := a;
  wSize.HighPart := b;
  e := wsize.QuadPart;
  Caption := Format('$%x $%x  $%x', [c, d, e]);

Note that in the expression for c 32-bit value is shifted by 32 bits left and looses set bit, then zero transforms to 64-bit.请注意,在c的表达式中,32 位值左移 32 位并丢失设置位,然后零转换为 64 位。

Unbound to how you get the filesize: it would even be faster if you'd use a type ( manual ) that exists for ~25 years already to assign the filesize directly to the function's result instead of using an intermediate variable:不受您如何获取文件大小的限制:如果您使用已经存在约 25 年的类型( 手动)将文件大小直接分配给函数的结果而不是使用中间变量,它甚至会更快:

  Int64Rec(result).Hi:= wfad.nFileSizeHigh;
  Int64Rec(result).Lo:= wfad.nFileSizeLow;
end;

In case this isn't obvious to anyone here's what the compilation looks like:如果这对任何人来说都不明显,那么编译如下所示:

带中间变量

Above: the intermediate variable w: LARGE_INTEGER first gets assigned the two 32bit parts and then is assigned itself to the function's result.上图:中间变量w: LARGE_INTEGER首先被分配了两个 32 位部分,然后将自身分配给函数的结果。 Cost: 10 instructions.费用:10 条指令。

直接在函数结果上使用记录

Above: the record Int64Rec is used to cast the function's result and assign both 32bit parts directly, without the need of any other variable.上图:记录Int64Rec用于转换函数的结果并直接分配两个 32 位部分,不需要任何其他变量。 Cost: 6 instructions.费用:6 条指令。

Environment used: Delphi 7.0 (Build 8.1), compiler version 15.0, Win32 executable, code optimization: on.使用环境:Delphi 7.0(Build 8.1),编译器版本15.0,Win32可执行,代码优化:开启。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM