我寫了一個使用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%,但我只是沒有意識到這是特定的特徵。 –
我發現了一個艱難的方式。 –