線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1496
推到 Plurk!
推到 Facebook!

讀取TIniFile的速度太慢

答題得分者是:h@visli
johnny2212
初階會員


發表:34
回覆:65
積分:39
註冊:2003-04-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-21 15:59:14 IP:61.226.xxx.xxx 未訂閱
由於我將大量的資料寫入MyIniFile中,如元件的各種特性質,導致讀取的速度太慢,我嘗試將MyIniFile寫入記憶體中 BackupIniFile[0]:=MyIniFile(BackupIniFile是外部變數) ,但是一旦將MyIniFile.Free,BackupIniFile[0]的資料就不見了,好像MyIniFile一定要讀取路徑的資料,也就是要讀取硬碟資料 請問我要如何才能加快讀取的速度,第一次讀取慢不要緊,但是希望重複讀取的時候,速度能加快
h@visli
資深會員


發表:103
回覆:429
積分:431
註冊:2004-02-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-21 17:18:05 IP:222.248.xxx.xxx 未訂閱
1、INIFile適合保存一些簡單的配置,當存儲的內容較多,體積變大後,讀取的速度是會慢。 2、您也可以考慮采用寫到註冊表中或XML文件中。 3、您要保存元件的各種特性,可以采用下面的方法:
 This example shows how to use the built-in component streaming 
 support to convert any component into a string and convert that 
 string back into a component.    function ComponentToString(Component: TComponent): string;    var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;        end;
  finally
    BinStream.Free
  end;
end;    function StringToComponent(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(nil);        finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;
或者采用 ReadComponentResFile, WriteComponentResFile 方法:
The following code defines a component class (TStreamable class) 
and a persistent class (TContainedClass) that is the type of a 
property on the component class. When a button is clicked, an 
instance of the component class is created, saved to a file, 
deleted, and then loaded from the file. By setting a breakpoint 
on the property setter of the TContainedClass SomeData property, 
you can watch how the streaming system sets property values when 
it reads them from a file.
The following unit defines the classes to stream:    unit StrmExmpl;
{$R-,T-,H ,X }
interface
uses Classes
type
TContainedClass = class(TPersistent)
private:
  FSomeData: Integer;      procedure SetSomeData(Value: Integer);      public:        constructor Create; override;      // Only published properties
  // are automatically streamed.
  published:        property SomeData: Integer read FSomeData write SetSomeData;    end;
TStreamableClass = class(TComponent)
private:
  FContainedClass: TContainedClass;
  public:        constructor Create(AOwner: TComponent); override;        destructor Destroy; override;      // Only published properties
  // are automatically streamed.
  published:        property ContainedClass: TContainedClass read FContainedClass write FContainedClass;    end;    implementation    procedure TContainedClass.SetSomeData(Value: Integer);
begin
{ Place a breakpoint here and notice how the data is streamed back. }
  FSomeData := Value;
end;
constructor TContainedClass.Create;
begin
  FSomeData := 42;
end;
constructor TStreamableClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FContainedClass = TContainedClass.Create;    end;
destructor TStreamableClass.Destroy;
begin
  FContainedClass.Free;
end;
initialization
  RegisterClasses([TContainedClass, TStreamableClass]);
finalization
end.    The following OnClick event handler should be added to a button 
on the application抯 main form. It causes the above classes to 
be streamed out and in. Note that you must add StrmExmpl to the 
uses clause of unit1.    procedure TForm1.Button1Click(Sender: TObject);    var
  AClassInstance: TStreamableClass;
begin
  AClassInstance := TStreamableClass.Create(nil);
  WriteComponentResFile('C:\Test', AClassInstance);
  FreeAndNil(AClassInstance);      AClassInstance := ReadComponentResFile('C:\Test', nil) as TStreamableClass;
  FreeAndNil(AClassInstance);
end;
---------------------------- 於Delphi K.Top之上 博采眾家之長, 奉獻綿薄之力 --------------------------- 發表人 - h@visli 於 2005/09/21 17:24:48 發表人 - h@visli 於 2005/09/21 17:27:37
------
------------------------
博采眾家之長,奉獻綿薄之力
------------------------
RedSnow
版主


發表:79
回覆:1322
積分:845
註冊:2003-12-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-23 14:17:54 IP:61.230.xxx.xxx 未訂閱
johnny2212 您好:    或許可以考慮使用 TMemIniFile 或是 TRegistry 7 天天敲鍵盤 v 時時按滑鼠 8 發表人 - RedSnow 於 2005/09/23 14:19:19
系統時間:2024-06-28 19:01:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!