### Bookstore

 Computer Math and Games in Pascal Lazarus, the complete guide (only a few left)

### Author Topic: A propose for TStringList.Find...  (Read 296 times)

#### edgarrod71

• New member
• Posts: 15
##### A propose for TStringList.Find...
« on: August 12, 2017, 01:28:27 am »
Code: Pascal  [Select]
1. function TStringList.Find(const S: string; out Index: Integer): Boolean;
2.
3. var
4.   L, R, I: Integer;
5.   CompareRes: PtrInt;
6. begin
7.   Result := false;
8.   Index:=-1;
9.   if Not Sorted then
10.     Raise EListError.Create(SErrFindNeedsSortedList);
11.   // Use binary search.
12.   L := 0;
13.   R := Pred(Count);  // Count - 1;
14.   if L<=R then  //  while (L<=R) do
15.   repeat    // repeat is 5-10% faster than while, so
16.     I := L + (R - L) shr 1; // div 2;  shr is faster than div...
17.     CompareRes := DoCompareText(S, Flist^[I].FString);
18.     if (CompareRes>0) then
19.       L := Succ(I); // I+1;  instead of adding, Succ or Pred only checks... so faster code.
20.     else begin
21.       R := Pred(I); // I-1;
22.       if (CompareRes=0) then begin
23.          Result := true;
24.          if (Duplicates<>dupAccept) then
25.             L := I; // forces end of while loop
26.       end;
27.     end;
28.   until L>R;
29.   Index := L;
30. end;

#### marcov

• Global Moderator
• Hero Member
• Posts: 5573
##### Re: A propose for TStringList.Find...
« Reply #1 on: August 12, 2017, 01:44:43 am »
So where is the benchmark code to compare ?

#### RAW

• Sr. Member
• Posts: 411
##### Re: A propose for TStringList.Find...
« Reply #2 on: August 12, 2017, 10:16:26 am »
If the normal TStringlist is too slow, then it should be no problem at all to find a good HashList...
There are several improved TStringlists and several HashLists out there...

Just pick one and play trial and error...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.6.4 FPC 3.0.2