在我看來,問題的一部分是,你循環2.5M * 4.5M次。你有沒有嘗試過使用TStringList而不是數組?如果你的陣列是不是TStringList中(比如SA1,SA2),您可以編寫代碼,例如這樣的:
var
i, j: integer;
begin
SA1.CaseSensitive:=false;
SA2.CaseSensitive:=false;
SA1.Sort;
SA2.Sort;
for i := 0 to SA2.Count-1 do
begin
while true do
begin
//if we delete all the SA1 items, no more processing is required
if SA1.Count=0 then
exit;
//find the occurrence of SA2[i] in SA1
SA1.Find(SA2[i], j);
//Check if the line at item j in SA1 contains the text of SA2[i]
if Pos (SA2[i], SA1[j]) > 0 then //yes, then we delete it
SA1.Delete(j)
else
if (j-1>=0) and (Pos (SA2[i], SA1[j-1]) > 0) then //else check the previous line to see if that has the text
SA1.Delete(j-1)
else
if (j+1<SA1.Count) and (Pos (SA2[i], SA1[j+1]) > 0) then //else check the next line
SA1.Delete(j+1)
else //otherwise break out of while loop
break;
end;
end;
end;
我們正在使用的情況下找到trings的不敏感(和排序)名單。它只運行一次4.5M項目列表 - 並在2.5M陣列中刪除項目(即SA1在循環中縮小)。在循環結束時,SA1將只包含您想要的字符串(SA2中不存在)。也許你應該嘗試一下,看看它是否適合你(並希望提高性能)?
希望這會有所幫助。
更新20170221:我更新了代碼,在刪除之前使用Find查找SA1中的SA2字符串索引。而且我還更新了代碼,以便SA1中的SA2字符串可能出現多次。我還更新了代碼以使Stringlist操作不區分大小寫。
那麼你的問題是什麼? –