DelphiのVCLのTMemoの文字列読み込みを高速化
DelphiのVCLでTMemoを使って大量の文字列の読み込みをするときに書くコードは一般に以下のようになります。
procedure StringToMemo(AMemo:TMemo;AString:String); // Memo1にAStringをコピーする
begin
AMemo.Lines.BeginUpdate;
AMemo.Lines.Text:=ASring;
AMemo.Lines.EndUpdate;
end;
あるいは,TStringListを介して巨大な文字列を渡すときには次のようになります。
procedure StringToMemo(AMemo:TMemo;AString:String); // Memo1にAStringをコピーする
var
lSL:TStringList;
begin
lSL:=TStringList.Create;
lSL.Text:=AString;
AMemo.Lines.Assign(lSL);
lSL.Free;
end;
Assignで渡すときには,BeginUpdate/EndUpdateは必要ないです。Assignのコードの内部で利用されています。
しかし,TMemoに渡す文字列が大きくなると,低速になってしまいます。
ネットで調べてみるとTMemoのLoadFromFileでテキストファイルを渡したときには高速に表示されることにがわかりました。それならば,LoadFromStreamを使って文字列を渡せばよいのではないかと考えました。
そこで考えたのが以下のコードです。
procedure StringToMemo(AMemo:TMemo;AString:String); // Memo1にAStringをコピーする
var
lStrStream: TStringStream;
begin
lStrStream:=TStringStream.Create(Astring);
AMemo.Lines.LoadFromStream(lStrStream);
lStrStream.Free;
end;
LoadFromStreamもBeginUpdate/EndUpdateは必要ないです。
これでVCLのTMemoでも高速に大量の文字列を読み込むことができるようになりました。
Author And Source
この問題について(DelphiのVCLのTMemoの文字列読み込みを高速化), 我々は、より多くの情報をここで見つけました https://qiita.com/p_kato/items/e1385cdae4c1a618c85a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .