erlang中并行快速排序的问题
我在 erlang 中编写快速排序时遇到问题。我正在做的是生成两个进程,然后阻止当前进程,直到我得到左右子数组的响应。得到这两个响应后,我向其父级发送一条消息,为其提供计算列表。 家长! {self(), Lone ++ [H] ++ Ltwo}
但是我在两个子进程中都遇到了获取 undef 的错误。 这是代码。
quick(Parent, []) -> Parent ! {self(), []};
quick(Parent, [H | T]) ->
Pone = spawn_link(main, quick, [ self(), [ X || X <- T, H >= X ] ]) ,
Ptwo = spawn_link(main, quick, [ self(), [ Y || Y <- T, H < Y ] ]) ,
receive
{Pone, Lone} ->
receive
{Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
end;
{Ptwo, Ltwo} ->
receive
{Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
end
end.
sortquick(List) ->
quick(self(), List).
称为:
main:sortquick([12,4,7,22,25]).
I am facing problem with writing quicksort in erlang. What I am doing is I am spawning two processes and then blocking the current process till I get the response from both the left and right sub-arrays. Getting both these responses I send a message to its parent giving it the computed list. Parent ! {self(), Lone ++ [H] ++ Ltwo}
But I am getting error of gettting undef in both the sub-processes.
Here is the code.
quick(Parent, []) -> Parent ! {self(), []};
quick(Parent, [H | T]) ->
Pone = spawn_link(main, quick, [ self(), [ X || X <- T, H >= X ] ]) ,
Ptwo = spawn_link(main, quick, [ self(), [ Y || Y <- T, H < Y ] ]) ,
receive
{Pone, Lone} ->
receive
{Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
end;
{Ptwo, Ltwo} ->
receive
{Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
end
end.
sortquick(List) ->
quick(self(), List).
called as:
main:sortquick([12,4,7,22,25]).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码本身不是问题。快速排序效果很好。您在子进程中得到 undef 的原因可能是由于函数 Quick/2 根本没有导出。当您使用模块和函数调用spawn_link时,需要导出该函数。
您可以通过添加
或将spawn_links更改为类似的内容
来解决此问题,但如果您采用后一种方式,则需要
在进行调用之前创建一个变量,否则它将不会返回到正确的进程。
The code itself is not the problem. The quick sort works fine. The reason, probably, why you are getting an undef in the subprocesses is due to the fact that the function quick/2 is not exported at all. When you call spawn_link with the module and function, the function needs to be exported.
You can fix this by either added
Or by changing the spawn_links to something like
Though if you do it the latter way, you'll need to create a variable
Before you make the calls or else it wont return to the proper process.
上面的代码通过导出 fast/2 函数可以正常工作。
后来我比较了生成的快速排序和未生成的快速排序的运行时间。
生成的快速排序需要 15 秒到 32 秒的时间来对范围 (1,1000000) 内的 100 万个随机数的列表进行排序。
未生成的快速排序是(下面的代码):
需要 5 秒到 8 秒之间的任何时间来对一百万个随机数的相同列表进行排序。
我正在我的旧 Thinkpad、1.7 Ghz 处理器(单核)和 512 Mb RAM 上测试代码。
有什么解释可以解释生成的快速排序比未生成的快速排序性能差吗?
The code above works fine by exporting quick/2 function.
Later on I compared the run time of the spawned quicksort vs the unspawned quicksort.
The spawned quicksort is taking anywhere between 15sec to 32 sec to sort a list of 1 million random numbers in range (1,1000000).
The unspawned quicksort is(code below):
takes anywhere between 5sec and 8sec to sort same list of a million random numbers.
I am testing code on my old Thinkpad, 1.7 Ghz processor (single core) and 512 Mb RAM.
Any explanation for spawned quicksort to perform poorer than unspawned one ?
我对代码进行了轻微修改,以防止它生成到许多进程。当它达到树中的某个级别时,它会切换到顺序。
I made a slight modification of the code to prevent it from spawning to many processes. When it reaches a certain level in the tree it switches to sequential.