Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
Delphi 10.4 Sydney
|
AW: Gibt es einen schnelleren Stringvergleich als if S1 = S2
16. Sep 2012, 12:05
Nein ich denke eher nur dann. Beispiel (hier schlägt der Vergleich fehl):
Delphi-Quellcode:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PString = ^TString;
TString = string[80];
TTest = class(TList)
public
procedure AddItem( const Value: TString);
procedure DelItem( Index: integer);
function CompareItems( const Index1, Index2: integer): boolean;
destructor Destroy; override;
end;
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TTest.AddItem( const Value: TString);
var
P: PString;
begin
New(P);
P^:= Value;
Add(P);
end;
procedure TTest.DelItem( Index: integer);
var
P: PString;
begin
P:= Items[ Index];
Dispose(P);
Delete( Index);
end;
destructor TTest.Destroy;
begin
while Count > 0 do
DelItem(Count - 1);
inherited Destroy;
end;
function TTest.CompareItems( const Index1, Index2: integer): boolean;
begin
Result:= CompareMem(Items[Index1], Items[Index2], SizeOf(TString));
end;
procedure TForm2.Button1Click(Sender: TObject);
var
Test: TTest;
begin
Test:= TTest.Create;
try
Test.AddItem(' TestItem');
Test.AddItem(' TestItem');
if Test.CompareItems(0, 1) then ShowMessage(' CompareItems');
finally
Test.Free;
end;
end;
end.
|
|
Zitat
|