C101_通过THashedStringList来写值和读值

01_通过THashedStringList来写值和读值




这是一个读取【Name=Delphi 学习大师】这种结构的示例。
这种结构INI文件最常见。


如图:   





代码:

unit Unit1;

interface

uses
   inifiles,
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
   TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
   private
    { Private declarations }
   public
    HashList:THashedStringList;
    { Public declarations }
   end;

var
   Form1: TForm1;

implementation



{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   HashList.Values['Name'] := 'Delphi 学习大师';
   HashList.Values['PassWord'] := 'QQ16643506';

   Memo1.Clear;
   Memo1.Lines.AddStrings(HashList);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   str: string;
begin
 str := HashList.Values['Name'];
 ShowMessage(str);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   HashList := THashedStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   HashList.DisposeOf;
end;

end.    




02_加载INI文件到THashedStringList并读值




这是一个读取INI文件并取值示例,演示了如何通过THashedStringList取值。
学习大师原创教程,请勿转载,侵权必究。








unit Unit1;

interface

uses
   inifiles,
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
   TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
   private
    { Private declarations }
   public
    HashList:THashedStringList;
    { Public declarations }
   end;

var
   Form1: TForm1;

implementation



{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   HashList.Values['Name'] := 'Delphi 学习大师';
   HashList.Values['PassWord'] := 'QQ16643506';

   Memo1.Clear;
   Memo1.Lines.AddStrings(HashList);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   str: string;
begin
 str := HashList.Values['Name'];
 ShowMessage(str);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
   vFileName: string;
   vIni:TIniFile;
begin
   vFileName := 'D:\Delphi 工程\教学1\HashedStringlist\Config.ini';
   vIni := TIniFile.Create(vFileName);
   try
    vIni.ReadSectionValues('System',HashList);
   finally
    vIni.DisposeOf;
   end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
   Memo1.Clear;
   Memo1.Lines.AddStrings(HashList);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   HashList := THashedStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   HashList.DisposeOf;
end;

end.   




03_测试THashedStringList和TStringList查询效率




认识THashedStringList查找效率:从100万行数据里面查找字符串行只需要0毫秒你信吗?





第一次查找:两个查询时间基本相同



第二次查找:List的查找和上次时间基本差不多,Hash的查找只有0毫秒,快的不可思议。



源码:


unit Unit1;

interface

uses
   IniFiles,
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin;

type
   TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
   private
    { Private declarations }
   public
    Hash: THashedStringList;
    List: TStringList;
    { Public declarations }
   end;

var
   Form1: TForm1;

implementation


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
   vFile: TextFile;
   vFileName: string;
   I: Integer;
begin
   vFileName := ExtractFileDir(ParamStr(0))+'\Test.txt';
   AssignFile(vFile, vFileName);
   Rewrite(vFile);//创建文件,或者使用ReSet打开文件
   for I := 1 to 1000000 do
   begin
    Writeln(vFile, '我是Delphi'+I.ToString);
   end;
   CloseFile(vFile);
   ShowMessage('创建完毕');
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
   Hash.LoadFromFile(ExtractFileDir(ParamStr(0))+'\Test.txt');
   List.LoadFromFile(ExtractFileDir(ParamStr(0))+'\Test.txt');
end;

procedure TForm1.Button3Click(Sender: TObject);
var
   T1,T2:Int64;
   I: integer;
begin
   T1 := GetTickCount;
   I := List.IndexOf('我是Delphi'+SpinEdit1.Value.ToString);
   T2 := GetTickCount;
   ShowMessage(format('List查询结果:%d,耗时%d',[I,T2-T1]));

   T1 := GetTickCount;
   I := Hash.IndexOf('我是Delphi'+SpinEdit1.Value.ToString);
   T2 := GetTickCount;
   ShowMessage(format('Hash查询结果:%d,耗时%d',[I,T2-T1]));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   Hash := THashedStringList.Create;
   List := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   Hash.DisposeOf;
   List.DisposeOf;
end;

end.   
 




联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678