测试效果图:

另外 TStringList.Find 方法也是使用了 "二分查找" 的办法.

二分查找太快了, 用 GetTickCount 测试不出来, 只好使用 QueryPerformanceCounter;unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {顺序查找函数} function SeqSearch(List: TStringList; const str: string): Integer; var i: Integer; begin for i := 0 to List.Count - 1 do if CompareText(List[i], str) = 0 then begin Result := i; Exit; end; Result := -1; end; {二分查找函数; 二分查找只能针对有序列表} function BinarySearch(List: TStringList; const str: string): Integer; var L,R,M: Integer; CompareResult: Integer; begin Result := -1; L := 0; R := List.Count - 1; while L <= R do begin M := (L + R) div 2; CompareResult := CompareText(List[M], str); if CompareResult < 0 then L := M + 1 else if CompareResult > 0 then R := M - 1 else begin Result := M; Exit; end; end; end; {对比测试} procedure TForm1.Button1Click(Sender: TObject); var TestList: TStringList; i: Integer; n1,n2: Int64; Count1,Count2: Integer; s: string; const num = 1000000; {准备测试百万个数据} begin TestList := TStringList.Create; for i := 0 to num-1 do TestList.Add(IntToHex(i,8)); {准备有序的测试值列表} Memo1.Clear; Count1 := 0; Count2 := 0; {搞 10 实验} for i := 0 to 9 do begin {产生范围内的随机字串} Randomize; s := IntToHex(Random(num),8); {顺序查找} QueryPerformanceCounter(n1); SeqSearch(TestList, s); QueryPerformanceCounter(n2); Memo1.Lines.Add(IntToStr(n2-n1)+ #9); Count1 := Count1 + (n2-n1); {二分查找} QueryPerformanceCounter(n1); BinarySearch(TestList, s); QueryPerformanceCounter(n2); Memo1.Lines[i] := Memo1.Lines[i] + IntToStr(n2-n1); Count2 := Count2 + (n2-n1); end; Memo1.Lines.Add('----------------'); Memo1.Lines.Add('平均值:'); Memo1.Lines.Add(IntToStr(Count1 div 10)+ #9 + IntToStr(Count2 div 10)); Memo1.Lines.Add('----------------'); Memo1.Lines.Insert(0, '顺序'#9'二分'); TestList.Free; end; end.
另外 TStringList.Find 方法也是使用了 "二分查找" 的办法.
分类:
算法与数据结构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧