Who said that Delphi has no hash? -- Performance Comparison Between tstringlist and thashedstringlist in Delphi
Many people have seen that Delphi does not have hash tables. These people are very conscious, but there are still many people who prove that Delphi is more spam than other languages...
Okay. Click it and switch to the question.
Tstringlist is a commonly used string list type. Its usage is not described here. However, when the number of data items increases, tstringlist searches (mainly name/key searches and indexof searches) the performance will drop sharply because the internal storage of tstringlist uses the linked list, while the search operation uses the loop Traversal method.
Fortunately, in the inifiles unit, Delphi provides us with the thashedstringlist type, that is, the tstringlist with hash processing. It inherits from the tstringlist and only optimizes the search method. Therefore, we can use it to replace the tstringlist when searching a large number of strings with confidence. All we need to change is to use thashedstringlist after: =. create to replace tstringlist. create, but the speed is increased by an order of magnitude.
To show the performance gap between the two, I wrote a few simple statements to test their respective features.
Unit ufrmmainform;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, strutils, inifiles;
Type
Tform1 = Class (tform)
Btn1: tbutton;
Mmo1: tmemo;
Procedure btn1click (Sender: tobject );
Private
... {Private Declarations}
Public
Procedure testthashstringlist;
Procedure testtstringlist;
... {Public declarations}
End;
VaR
Form1: tform1;
Implementation
Uses dateutils;
... {$ R *. DFM}
Procedure tform1.btn1click (Sender: tobject );
Begin
Testtstringlist;
Mmo1.lines. Add ('--------------------');
Testthashstringlist;
End;
Procedure tform1.testthashstringlist;
VaR
HSL: thashedstringlist;
I: integer;
Datepointer: tdatetime;
Hsltime, hsltimec, hsltimei: int64;
Tempint: integer;
Tempstr: string;
Begin
Hsltime: = 0;
Hsltimec: = 0;
Datepointer: = now ();
HSL: = thashedstringlist. Create ();
For I: = 1 to 200000 do
Begin
HSL. Add ('string _ '+ inttostr (I ));
End;
Hsltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st thashedstringlist Creation Time: '+ inttostr (hsltime) + 'millisecond ');
Datepointer: = now ();
For I: = 0 to 200 do
Begin
Tempint: = HSL. indexof ('string _ '+ inttostr (I * 1000 ));
End;
Hsltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1970 string SEARCH thashedstringlist time consumed: '+ inttostr (hsltime) + 'millisecond ');
Datepointer: = now;
For I: = 1 to 200000 do
Begin
Tempstr: = HSL. Strings [I-1];
End;
Hsltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1970 index search thashedstringlist time consumed: '+ inttostr (hsltime) + 'millisecond ');
HSL. Clear;
Datepointer: = now;
For I: = 1 to 100000 do
Begin
HSL. insert (I-1, 'string _ '+ inttostr (I ));
End;
Hsltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st thashedstringlist insertion time with location: '+ inttostr (hsltime) + 'millisecond ');
Datepointer: = now;
For I: = 1 to 100000 do
Begin
HSL. insert (0, 'string _ '+ inttostr (I ));
End;
Hsltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st thashedstringlist 0 position insertion time: '+ inttostr (hsltime) + 'millisecond ');
End;
Procedure tform1.testtstringlist;
VaR
SL: tstringlist;
I: integer;
Datepointer: tdatetime;
Sltime, sltimec, sltimei: int64;
Tempint: integer;
Tempstr: string;
Begin
Sltime: = 0;
Sltimec: = 0;
Datepointer: = now ();
SL: = tstringlist. Create;
For I: = 1 to 200000 do
Begin
SL. Add ('string _ '+ inttostr (I ));
End;
Sltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st tstringlist Creation Time: '+ inttostr (sltime) + 'millisecond ');
Datepointer: = now;
For I: = 0 to 200 do
Begin
Tempint: = SL. indexof ('string _ '+ inttostr (I * 1000 ));
End;
Sltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1970 string SEARCH tstringlist time consumed: '+ inttostr (sltime) + 'millisecond ');
Datepointer: = now;
For I: = 1 to 200000 do
Begin
Tempstr: = SL. Strings [I-1];
End;
Sltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1970 index search tstringlist time consumed: '+ inttostr (sltime) + 'millisecond ');
Datepointer: = now;
For I: = 1 to 100000 do
Begin
SL. insert (I-1, 'string _ '+ inttostr (I ));
End;
Sltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st tstringlist insertion time with location: '+ inttostr (sltime) + 'millisecond ');
Datepointer: = now;
For I: = 1 to 100000 do
Begin
SL. insert (0, 'string _ '+ inttostr (I ));
End;
Sltime: = millisecondsbetween (now, datepointer );
Mmo1.lines. Add ('1st tstringlist 0 position insertion time: '+ inttostr (sltime) + 'millisecond ');
End;
End.
Compile and run the program. The running result is as follows:
200000 tstringlist Creation Time: 109 milliseconds
200 string SEARCH tstringlist time: 11514 milliseconds
200000 index search tstringlist time: 31 Ms
100000 tstringlist insertion time with location: 85828 milliseconds
100000 tstringlist 0-position insertion time: 148734 milliseconds
--------------------
200000 thashedstringlist Creation Time: 108 milliseconds
200 string SEARCH thashedstringlist time: 188 milliseconds
200000 index search thashedstringlist time: 15 ms
100000 thashedstringlist insertion time with location: 63 MS
100000 thashedstringlist 0-position insertion time: 55640 milliseconds
(Test environment: Windows2003 SP1 + Delphi 2006 Update 2 HotFix1-9 + 1g memory + P-D2.8G dual-core)
The results are obvious. There is no difference in the performance when the two are created (thashedstringlist is slower than tstringlist In addition and insertion operations, which is not shown here, I don't know if thashedstringlist is not what people have said. Do hash operations every time you insert an item. Please kindly advise ), among the 200 string searches (from 1 to 2000000 evenly searched items), the performance of the two is so poor. Subsequent tests are also expected.
It can be seen that thashedstringlist can better replace the commonly used tstringlist to work. Why should we refuse?
In addition, the inifiles unit also provides tstringhash, but only performs hash operations on the key, which is generally not commonly used.