Test:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; memo1: tmemo; procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} {sequential query function} function seqsearch (list: tstringlist; const STR: string): integer; var I: integer; begin for I: = 0 to list. count-1 do if comparetext (list [I], STR) = 0 then begin result: = I; exit; end; Result: =-1; end; {binary search function; binary Search can only target ordered list} function binarysearch (list: tstringlist; const STR: string): integer; var L, R, M: integer; compareresult: integer; begin result: =-1; L: = 0; R: = List. count-1; while l 0 then R: = m-1 else begin result: = m; exit; end; {comparison test} procedure tform1.button1click (Sender: tobject); var testlist: tstringlist; I: integer; N1, N2: int64; count1, count2: integer; s: string; const num = 1000000; {prepare to test millions of data records} begin testlist: = tstringlist. create; for I: = 0 to num-1 do testlist. add (inttohex (I, 8); {prepare an ordered list of test values} memo1.clear; count1: = 0; count2: = 0; {engage in 10 experiments} For I: = 0 to 9 do begin {random string within the generation range} randomize; s: = inttohex (random (Num), 8); {sequential lookup} querycececounter (N1 ); seqsearch (testlist, S); queryperformancecounter (N2); memo1.lines. add (inttostr (n2-n1) + #9); count1: = count1 + (n2-n1); {binary lookup} queryperformancecounter (N1); binarysearch (testlist, S ); queryperformancecounter (N2); memo1.lines [I]: = memo1.lines [I] + inttostr (n2-n1); count2: = count2 + (n2-n1); end; memo1.lines. add ('----------------'); memo1.lines. add ('average: '); memo1.lines. add (inttostr (count1 Div 10) + #9 + inttostr (count2 Div 10); memo1.lines. add ('----------------'); memo1.lines. insert (0, 'ordered '# 9' binary'); testlist. free; end.
The binary search is too fast and cannot be tested using gettickcount. Therefore, queryperformancecounter must be used;
In addition, the tstringlist. Find method also uses the "binary search" method.