算法优化,一段理论上可行的代码,求优化。
begin SetLength(str,Memo2.Lines.Count,2); for I := 0 to Memo2.Lines.Count - 1 do begin str2:=SplitString(Memo2.Lines[i],#9); str[I,0]:=str2[0]; str[I,1]:=str2[1]; end; for i := 0 to Memo1.Lines.Count - 1 do begin if Memo1.Lines[i]<>'' then begin ReStr:=''; for n := low(str) to High(str) do begin if Trim(Memo1.Lines[i])=str[n,0] then begin ReStr:=Memo1.Lines[i]+#9+str[n,1]; Continue; end; end; if ReStr<>'' then begin Memo1.Lines[i]:=ReStr; end else begin Memo1.Lines[i]:=Memo1.Lines[i]+#9+'未找到数据'; end; Application.ProcessMessages; Gauge1.MinValue:=0; Gauge1.MaxValue:=Memo1.Lines.Count; Gauge1.Progress:=gauge1.Progress+1; end; end;end;上面是本人写的,数据量小的时候还行,但是数据上万条以后,基本上就无法使用了,具体情况如下:memo1中内容AA01AA01AA02AA03AA05AA10AA02AA02。。。。。至万条memo2中内容AA01 张三AA02 王是AA03 李而AA04 李而AA05 李而AA06 李XAA07 李VAA08 李SAA09 李WAA10 李XXXAA11 李SSAA12 李WWAA13 李RRAA14 李AAAA15 李QQAA16 李TTAA17 李YY至。。。。几百条比对完毕的结果在memo1中显示为AA01 张三AA01 张三AA02 王是AA03 李而AA05 李UAA10 李XXXAA02 王是AA02 王是求大侠给优化一下,另外不用数据库什么的。单纯程序内比对。谢谢
procedure TForm1.Button1Click(Sender: TObject);const space=' ';{这里的空格根据你的Memo2中ID和Name之间空格决定,自己改合适的}var i,j:Integer; TmpStr:string; sList:TStringList;begin sList:=TStringList.Create; try for i:=0 to Memo1.Lines.Count-1 do begin TmpStr:=Memo1.Lines[i]+space; j:=pos(TmpStr, Memo2.Text); if j >0 then TmpStr:=Memo2.Lines[SendMessage(Memo2.Handle,EM_LINEFROMCHAR,j,0)] else TmpStr:=Memo1.Lines[i]+space+'未找到' ; sList.Add(TmpStr) end; Memo1.Lines.Assign(sList); finally sList.Free; end;end;