gpt4 book ai didi

delphi - 如何在 Delphi TStringList 中更快地搜索名称/值对?

转载 作者:行者123 更新时间:2023-12-03 14:55:01 25 4
gpt4 key购买 nike

我通过将运行时的所有字符串放入 TStringList 中,在应用程序中实现了语言翻译:

procedure PopulateStringList;
begin
EnglishStringList.Append('CAN_T_FIND_FILE=It is not possible to find the file');
EnglishStringList.Append('DUMMY=Just a dummy record');
// total of 2000 record appended in the same way
EnglishStringList.Sorted := True; // Updated comment: this is USELESS!
end;

然后我使用以下方法获得翻译:

function GetTranslation(ResStr:String):String;
var
iIndex : Integer;
begin
iIndex := -1;
iIndex := EnglishStringList.IndexOfName(ResStr);
if iIndex >= 0 then
Result := EnglishStringList.ValueFromIndex[iIndex] else
Result := ResStr + ' (Translation N/A)';
end;

无论如何,使用这种方法大约需要 30 微秒来定位记录,是否有更好的方法来实现相同的结果?

更新:为了将来引用,我在这里编写了按照建议使用 TDictionary 的新实现(适用于 Delphi 2009 及更高版本):

procedure PopulateStringList;
begin
EnglishDictionary := TDictionary<String, String>.Create;
EnglishDictionary.Add('CAN_T_FIND_FILE','It is not possible to find the file');
EnglishDictionary.Add('DUMMY','Just a dummy record');
// total of 2000 record appended in the same way
end;


function GetTranslation(ResStr:String):String;
var
ValueFound: Boolean;
begin
ValueFound:= EnglishDictionary.TryGetValue(ResStr, Result);
if not ValueFound then Result := Result + '(Trans N/A)';
end;

新的 GetTranslation 函数的执行速度(在我的 2000 条样本记录上)比第一个版本快 1000 倍。

最佳答案

THashedStringList我想应该会更好。

关于delphi - 如何在 Delphi TStringList 中更快地搜索名称/值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3709784/

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