我有一个简单的程序程序。转换程序为多线程,利用多核cpu
Procedure TForm1.btnKeywrdTransClick(Sender: TObject);
Var
i, ii : integer;
ch_word, zword, uy_word: widestring;
Begin
TntListBox1.items.LoadFromFile('d:\new folder\chh.txt'); //Chinese
TntListBox2.items.LoadFromFile('d:\new folder\uyy.txt'); //Uyword
TntListBox4.items.LoadFromFile(Edit3.text); //list of poi files
For I := 0 To TntListBox4.items.Count - 1 do
Begin
TntListBox3.items.LoadFromFile(TntListBox4.Items[i]);
zword := tntlistbox3.Items.Text; //Poi
For ii := 0 To TntListBox1.Items.count - 1 Do
Begin
loopz;
ch_word := tntlistbox1.Items[ii];
uy_word := ' ' + TntListBox2.items[ii] + ' ';
zword := wideFastReplace(zword, ch_word, uy_word, [rfReplaceAll]); //fastest, and better for large text
End;
TntListBox3.Items.text := zword;
TntListBox3.items.SaveToFile(TntListBox4.Items[i]);
end;
end;
现在我的新电脑有4cores,使得这一程序的多线程将使其运行速度更快(如果我使用4个线程,每核心线程)? 我没有多线程的经验,我需要你的帮助 谢谢。
PS:这是LOOPZ过程
Procedure loopz;
Var
msg : tmsg;
Begin
While PeekMessage(Msg, 0, 0, 0, pm_Remove) Do
Begin
If Msg.Message = wm_Quit Then Halt(Msg.WParam);
TranslateMessage(Msg);
DispatchMessage(Msg);
End;
End;
更新1:答案 ,我也会做
1 - 使用一个分析器,以找到最耗时的代码
2 - 尽可能消除与gui相关的东西
3 - 使用线程。
我会报告回来。谢谢大家。
有一个简单的方法来并行化代码,看看我的答案。最大的问题是给名单上的专有名称,以便每个人都能看到并理解该算法实际上在做什么! – 2010-06-19 08:19:21
@Cosmin:代码执行zword:= replace(zword,a,b,[all]),并没有简单的方法来并行化。 – gabr 2010-06-19 08:50:46
你不会并行化;您将第一个循环(经过文件名的循环)并行化。 – 2010-06-19 08:57:37