gpt4 book ai didi

delphi - THashedStringList 查找项目的更高效替代方案

转载 作者:行者123 更新时间:2023-12-02 01:54:40 25 4
gpt4 key购买 nike

我发现我正在维护的应用程序存在性能瓶颈,其中使用了一种奇怪的技术来本地化用户界面。

名称值对存储在一个相当大的THashedStringList中,并且瓶颈由所有调用给出(分析器结果)

THashedStringList.IndexOfName

THashedStringList.ValueFromIndex

不知何故,我希望有一个更快的类,可以以某种方式使用类似的签名(indexOfNameValueFromIndex),这样我就可以在不重写此代码的情况下实现性能改进奇怪但有效的本地化机制。我认为这段代码最初是用 Delphi 2009 编写的,但现在我有西雅图。

谢谢。

最佳答案

我不能在这里发布代码(只是界面),因为它太大了,但是如果你继续 alcinoe ( https://github.com/Zeus64/alcinoe ),你就会有一组类似 TStringlist 的组件,尤其是一个名为 TALHashedStringList 的组件,它可以完全工作与 THashedStringList 类似,但在内部它使用 TDictionnary(因此现在 TALHashedStringList.IndexOfName 和 TALHashedStringList.ValueFromIndex 所花费的时间必须为 null)

  {----------------------------------}
TALHashedStringList = class(TALStrings)
private
FNodeList: TObjectList<TALHashedStringListDictionaryNode>;
FDictionary: TALObjectDictionary<ansiString, TALHashedStringListDictionaryNode>;
FDuplicates: TDuplicates;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
FOwnsObject: Boolean;
FCaseSensitive: boolean;
procedure ExchangeItems(Index1, Index2: Integer);
procedure QuickSort(L, R: Integer; SCompare: TALHashedStringListSortCompare);
procedure SetCaseSensitive(const Value: Boolean);
function GetCaseSensitive: Boolean;
Function ExtractNameValue(const S: AnsiString; var Name, Value: AnsiString): Boolean;
procedure SetDuplicates(const Value: TDuplicates);
function CreateDictionary(ACapacity: integer; aCaseSensitive: boolean): TALObjectDictionary<ansiString, TALHashedStringListDictionaryNode>;
protected
function GetName(Index: Integer): AnsiString; override;
function GetStrictName(Index: Integer): AnsiString; override; // [added from Tstrings]
function GetValue(const Name: AnsiString): AnsiString; override;
procedure SetValue(const Name, Value: AnsiString); override;
function GetValueFromIndex(Index: Integer): AnsiString; override;
procedure SetValueFromIndex(Index: Integer; const Value: AnsiString); override;
procedure SetPersistentValue(const Name, Value: AnsiString); override; // [added from Tstrings]
procedure SetPersistentValueFromIndex(Index: Integer; const Value: AnsiString); override; // [added from Tstrings]
procedure Changed; virtual;
procedure Changing; virtual;
function Get(Index: Integer): AnsiString; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
function GetTextStr: AnsiString; override;
procedure Put(Index: Integer; const S: AnsiString); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: Integer); override;
procedure SetUpdateState(Updating: Boolean); override;
procedure InsertItem(Index: Integer; const Name, Value: AnsiString; AObject: TObject); overload; virtual;
procedure InsertItem(Index: Integer; const S: AnsiString; AObject: TObject); overload; virtual;
procedure AssignTo(Dest: TPersistent); override; //[added from Tstrings]
procedure init(OwnsObjects: Boolean; ACapacity: Integer); virtual; //[added from TStringList]
public
constructor Create; overload; override;
constructor Create(OwnsObjects: Boolean); reintroduce; overload;
constructor Create(ACapacity: Integer); reintroduce; overload; //[added from Tstrings]
constructor Create(OwnsObjects: Boolean; ACapacity: Integer); reintroduce; overload; //[added from Tstrings]
destructor Destroy; override;
function Add(const S: AnsiString): Integer; override;
function AddObject(const S: AnsiString; AObject: TObject): Integer; override;
function AddNameValue(const Name, Value: AnsiString): Integer; override; // [added from Tstrings]
function AddNameValueObject(const Name, Value: AnsiString; AObject: TObject): Integer; override; // [added from Tstrings]
procedure Assign(Source: TPersistent); override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
function ExtractObject(Index: Integer): TObject; overload; virtual;
procedure Exchange(Index1, Index2: Integer); override;
function IndexOf(const S: AnsiString): Integer; override;
function IndexOfName(const Name: AnsiString): Integer; override; // [added from TStringList]
procedure Insert(Index: Integer; const S: AnsiString); override;
procedure InsertObject(Index: Integer; const S: AnsiString; AObject: TObject); override;
procedure InsertNameValue(Index: Integer; const Name, Value: AnsiString); override; // [added from Tstrings]
procedure InsertNameValueObject(Index: Integer; const Name, Value: AnsiString; AObject: TObject); override; // [added from Tstrings]
procedure Move(CurIndex, NewIndex: Integer); override;
procedure CustomSort(Compare: TALHashedStringListSortCompare); virtual;
property Duplicates: TDuplicates read FDuplicates write SetDuplicates;
property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
property OwnsObjects: Boolean read FOwnsObject write FOwnsObject;
end;

您可以通过此演示查看性能: https://svn.code.sf.net/p/alcinoe/code/demos/ALSortedListBenchmark/win32/ALSortedListBenchmark.exe

关于delphi - THashedStringList 查找项目的更高效替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com