最好像手工一样,用手指顺着每个列表向下移动。
这是执行此操作的过程。
procedure TFormTest.CompareLists(const Lista, Listb, Listc: TStrings);
var
a, b, amax, bmax : integer;
begin
aMax := Lista.Count;
bMax := Listb.Count;
Listc.Clear;
a:=0;
b:=0;
while (a< aMax) and (b<bMax) do
begin
// Once either index goes past end of list we are done;
case Sign( CompareStr( Lista[a], Listb[b])) of // oer CompareText if not case sensitive
-1:
begin
// ista[a] < List0[ b ]b
inc( a );
end;
0:
begin
// a = b
Listc.add( Lista[a] ); // same so could be either
inc(a);
inc(b);
end;
1:
begin
inc( b );
end;
end;
end;
end;
您可以将其直接复制到您的程序中。
它还可以轻松比较是否值得复制到外部列表。我这样做了,发现使用列表框条目大约需要 4 秒,因为 35000 是条目(比 4 个字符长得多),而对于单独的列表则不到一秒。这种差异远远超过了将项目复制到单独的字符串列表所花费的时间,但两次都比你所取得的要好得多。
这是完整的测试单元:
unit UnitTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Math, Vcl.StdCtrls;
type
TFormTest = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
EditCompareLists: TEdit;
EditCompareListBoxes: TEdit;
ButtonTest: TButton;
EditCount1: TEdit;
EditCount2: TEdit;
EditCount3: TEdit;
procedure ButtonTestClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure CompareLists( const Lista, Listb, Listc : TStrings );
end;
var
FormTest: TFormTest;
implementation
{$R *.dfm}
{ TForm1 }
procedure TFormTest.ButtonTestClick(Sender: TObject);
var
List1, List2, List3 : TStringList;
iTime : TTime;
i: Integer;
begin
// 1. Generate lists - should be string[4] but if bigger should slow things down
List1 := TStringList.Create;
Try
List2 := TStringList.Create;
try
List3 := TStringList.Create;
try
List1.Sorted := TRUE;
List2.Sorted := TRUE;
List1.Duplicates := dupIgnore;
List2.Duplicates := dupIgnore;
for i := 1 to 50000 do
begin
List1.Add( IntToStr( Random( 65000 )));
List2.Add( IntToStr( Random( 65000 )));
end;
ListBox1.Items.Assign( List1 );
ListBox2.Items.Assign( List2 );
EditCount1.Text := IntToStr(ListBox1.Items.Count );
EditCount2.Text := IntToStr(ListBox2.Items.Count );
// now see how fast eachcan be sorted
// first - list boxes
iTime := Now;
CompareLists( ListBox1.Items, ListBox2.Items, ListBox3.Items );
iTime := Now - iTime;
EditCompareListBoxes.Text := TimeToStr( iTime );
EditCount3.Text := IntToStr(ListBox3.Items.Count );
// and Lists
iTime := Now;
CompareLists( List1, List2, List3 );
iTime := Now - iTime;
EditCompareLists.Text := TimeToStr( iTime );
finally
List3.Free;
end;
finally
List2.Free;
end;
Finally
List1.Free;
End;
end;
procedure TFormTest.CompareLists(const Lista, Listb, Listc: TStrings);
var
a, b, amax, bmax : integer;
begin
aMax := Lista.Count;
bMax := Listb.Count;
Listc.Clear;
a:=0;
b:=0;
while (a< aMax) and (b<bMax) do
begin
// Once either index goes past end of list we are done;
case Sign( CompareStr( Lista[a], Listb[b])) of // oer CompareText if not case sensitive
-1:
begin
// ista[a] < List0[ b ]b
inc( a );
end;
0:
begin
// a = b
Listc.add( Lista[a] ); // same so could be either
inc(a);
inc(b);
end;
1:
begin
inc( b );
end;
end;
end;
end;
end.