德尔福文本文件的写作表演 [英] Text File Writing performances in Delphi

查看:133
本文介绍了德尔福文本文件的写作表演的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我软处理传入的字符串(来自Telnet或HTTP或...),我必须用Delphi XE2编写文本文件,以便处理字符串。由于有时字符串可能会崩溃的程序,我需要确保在我的文件中的字符串。所以我打开/关闭每个传入的字符串的文件,我有一些性能问题。通常(对于我的代码测试)8秒为

我的代码在这里,有没有办法来改善保持函数的性能?
(为了测试,只需创建一个Button:Button1,带有OnClick事件和一个Label:lbl1)。

 <$ c $ AddToFile(Source:string; FileName:String); 
var
FText:Text;
TmpBuf:字节的数组[字]
开始
{$ I-}
AssignFile(FText,FileName);
追加(FText);
SetTextBuf(FText,TmpBuf);
Writeln(FText,Source);
CloseFile(FText);
{$ I +}
end;

过程initF(FileName:string);
Var FText:text;
begin
{$ I-}
if FileExists(FileName)then DeleteFile(FileName);
AssignFile(FText,FileName);
ReWrite(FText);
CloseFile(FText);
{$ I +}
end;

procedure TForm1.Button1Click(Sender:TObject);
var tTime:TDateTime;
iBcl:Integer;
文件名:字符串;
begin
FileName:='c:\Test.txt';
lbl1.Caption:='Go->'+ FileName; lbl1.Refresh;
initF(FileName);
tTime:=现在;
对于iBcl:= 0到2000 do
AddToFile(IntToStr(ibcl)+''+'lkjlkjlkjlkjlkjlkjlkj',FileName);
lbl1.Caption:= FormatDateTime('sss:zzz',Now-tTime);
end;


解决方案

使用 TStreamWriter ,它会自动缓冲,并且可以将缓冲区自动清空到 TFileStream 。它还允许您选择追加到现有的文件,如果需要的话,为Unicode支持设置字符编码,并允许您在其各种重载 创建 构造函数(注意,刷新 TStreamWriter )只写入 TStreamBuffer 的内容。

code> TFileStream ;它不刷新操作系统文件系统缓冲区,所以文件实际上并没有写在磁盘上,直到 TFileStream 被释放。)



不要每次创建StreamWriter;只需创建并打开一次,并在最后关闭它:

pre $函数InitLog(const FileName:string)
begin
结果:= TStreamWriter.Create(FileName,True);
Result.AutoFlush:= True; //写完后自动刷新
Result.NewLine:= sLineBreak; //使用系统换行符
end;

过程CloseLog(const StreamWriter:TStreamWriter);
begin
StreamWriter.Free;
end;

procedure TForm1.Button1Click(Sender:TObject);
var
tTime:TDateTime;
iBcl:Integer;
LogSW:TStreamWriter;
文件名:TFileName;
begin
FileName:='c:\Test.txt';
LogSW:= InitLog(FileName);
试试
lbl1.Caption:='Go->'+ FileName;
lbl1.Refresh;
tTime:=现在;

对于iBcl:= 0到2000 do
LogSW.WriteLine(IntToStr(ibcl)+''+'lkjlkjlkjlkjlkjlkjlkj');

lbl1.Caption:= FormatDateTime('sss:zzz',Now - tTime);
终于
CloseLog(LogSW);
end;
end;


My soft treat incoming strings (from Telnet or HTTP or...), and I have to write text file with Delphi XE2 for having a trace of incomming treated strings. As sometimes the string may crash the program I need to be sure to have the string in my file. So I open/close the file for every incoming string and I have some performance problems. Typically (for my code test) 8 seconds for

My code is here, is there a way to improve the perfs keeping the function ? (For test just create a Form with a Button : Button1, with OnClick event & a Label : lbl1).

Procedure AddToFile(Source: string; FileName :String);
var
  FText : Text;
  TmpBuf: array[word] of byte;
Begin
  {$I-}
  AssignFile(FText, FileName);
  Append(FText);
  SetTextBuf(FText, TmpBuf);
  Writeln(FText, Source);
  CloseFile(FText);
  {$I+}
end;

procedure initF(FileName : string);
Var  FText : text;
begin
  {$I-}
  if FileExists(FileName) then  DeleteFile(FileName);
  AssignFile(FText, FileName);
  ReWrite(FText);
  CloseFile(FText);
  {$I+}
end;

procedure TForm1.Button1Click(Sender: TObject);
var tTime : TDateTime;
    iBcl : Integer;
    FileName : string;
begin
  FileName := 'c:\Test.txt';
  lbl1.Caption := 'Go->' + FileName; lbl1.Refresh;
  initF(FileName);
  tTime := Now;
  For iBcl := 0 to 2000 do
    AddToFile(IntToStr(ibcl) + '   ' +  'lkjlkjlkjlkjlkjlkjlkj' , FileName);
  lbl1.Caption  :=  FormatDateTime('sss:zzz',Now-tTime);
end;

解决方案

Use a TStreamWriter, which is automatically buffered, and can handle flushing its buffers to the TFileStream automatically. It also allows you to choose to append to an existing file if you need to, set character encodings for Unicode support, and lets you set a different buffer size (the default is 1024 bytes, or 1K) in its various overloaded Create constructors.

(Note that flushing the TStreamWriter only writes the content of the TStreamBuffer to the TFileStream; it doesn't flush the OS file system buffers, so the file isn't actually written on disk until the TFileStream is freed.)

Don't create the StreamWriter every time; just create and open it once, and close it at the end:

function InitLog(const FileName: string): TStreamWriter;
begin
  Result := TStreamWriter.Create(FileName, True);
  Result.AutoFlush := True;         // Flush automatically after write
  Result.NewLine := sLineBreak;     // Use system line breaks
end;

procedure CloseLog(const StreamWriter: TStreamWriter);
begin
  StreamWriter.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var 
  tTime : TDateTime;
  iBcl : Integer;
  LogSW: TStreamWriter;
  FileName: TFileName;
begin
  FileName := 'c:\Test.txt';
  LogSW := InitLog(FileName);
  try
    lbl1.Caption := 'Go->' + FileName; 
    lbl1.Refresh;
    tTime := Now;

    For iBcl := 0 to 2000 do
      LogSW.WriteLine(IntToStr(ibcl) + '   ' +  'lkjlkjlkjlkjlkjlkjlkj');

    lbl1.Caption  :=  FormatDateTime('sss:zzz',Now - tTime);
  finally
    CloseLog(LogSW);
  end;
end;

这篇关于德尔福文本文件的写作表演的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆