If you want something fast, do not store your data in TListView.
Use TStringList to store the list, then use TListView in virtual mode.
Reading from TStringList.Items [] is many times faster than reading from the TListView.Items [] property.
If you are sure that there is no void element in the list, the following is used:
procedure Extract(List, Dest: TStrings; Char1, Char2: char); var i,j: integer; V: cardinal; type PC = {$ifdef UNICODE}PCardinal{$else}PWord{$endif}; begin V := ord(Char1)+ord(Char2) shl (8*sizeof(char)); Dest.BeginUpdate; Dest.Clear; for i := 0 to List.Count-1 do begin if PC(pointer(List[i]))^=V then begin for j := i to List.Count-1 do begin Dest.Add(List[j]); if PC(pointer(List[j]))^<>V then break; // end the for j := loop end; break; // end the for i := loop end; Dest.EndUpdate; end;
You can use binary search to get it even faster. But with the PWord () trick in the list of 50,000 items, you won't notice it.
Note that PC (pointer (List [i])) ^ = V is a faster version of the copy (List [i], 1,2) = Char1 + Char2, because no time line is created during the comparison. But it only works if List [i] = '', that is, there is no pointer (List [i]) = nil.
I added {$ ifdef UNICODE} and sizeof (char) so that this code compiles with the whole version of Delphi (before and after Delphi 2009).
source share