我写了一个使用Free Pascal和Lazarus IDE的程序。简而言之,它递归地扫描目录,并为每个文件“做些东西”(哈希),然后将哈希值和文件名输出到一个StringGrid中,每个后续文件都刷新一次。为什么我的StringGrid在成千上万的条目后似乎变慢了?免费Pascal
它可以很好地处理多达几千个文件,但是当你达到数万个时,它确实会变慢,每半秒处理一个文件,即使它只是一个几Kb的小文件。
下面是代码的主要部分。任何人都可以看到为什么我的程序在网格中的文件数量超过数万时变慢?
procedure TForm1.HashFile(FileIterator: TFileIterator);
var
SizeOfFile : int64;
NameOfFileToHash, fileHashValue, PercentageProgress : string;
FI : TFileIterator; //File Iterator class
SG : TStringGrid;
begin
FI := TFileIterator.Create;
SG := TStringGrid.Create(self);
SizeOfFile := 0;
fileHashValue := '';
if StopScan = FALSE then // If Stop button clicked, cancel scan
begin
NameOfFileToHash := (FileIterator.FileName);
SizeOfFile := FileSize(NameofFileToHash);
StatusBar1.SimpleText := 'Currently Hashing: ' + NameOfFileToHash;
fileHashValue := CalcTheHashFile(NameOfFileToHash); // Custom function, see below
// Now lets update the stringgrid and text fields
// StringGrid Elements:
// Col 0 is FileCounter. Col 1 is File Name. Col 2 is Hash
StringGrid1.rowcount:= FileCounter+1;
StringGrid1.Cells[0,FileCounter] := IntToStr(FileCounter);
Stringgrid1.Cells[1,FileCounter] := NameOfFileToHash;
Stringgrid1.Cells[2,FileCounter] := UpperCase(fileHashValue);
// Dynamically scroll the list so the user always has the most recently hashed
// file insight and expand the columns in lie with their content width
StringGrid1.row := FileCounter;
StringGrid1.col := 1;
StringGrid1.AutoSizeColumns;
// Progress Status Elements: Most of these vars are global vars
NoOfFilesExamined.Caption := IntToStr(FileCounter);
PercentageProgress := IntToStr((FileCounter * 100) DIV NoOfFilesInDir2);
Edit1.Caption := PercentageProgress + '%';
TotalBytesRead := TotalBytesRead + SizeOfFile;
edtTotalBytesExamined.Caption := FormatByteSize(TotalBytesRead);
Application.ProcessMessages;
FileCounter := FileCounter+1;
end;
SG.Free;
FI.Free;
end;
完整的源代码可以从我的SourceForge的网页,https://sourceforge.net/projects/quickhash/在“文件” - >“源代码”,如果你需要的。
任何帮助表示赞赏
特德
HiTaking的自动大小真的帮助!在我到目前为止的测试中,速度提高了约30%,或者换句话说,当我添加该功能时,它似乎已经减慢了30%,但我只是没有意识到这是特定的特征。 –
我发现了一个艰难的方式。 –