我是 Erlang 的新手,所以为了培训我尝试从头开始实现标准功能。我试图从列表模块创建 map/2函数的并行实现。但是我的实现工作非常缓慢。如果我在实施过程中犯了任何主要错误,您能否指出我:
-module( my_pmap ).
-export([ pmap/2 ]).
-export([ map/4, collect/3 ]).
map( F, Value, Indx, SenderPid ) ->
SenderPid ! { Indx, F( Value ) }.
pmap( F, List ) ->
CollectorPid = spawn_link( my_pmap, collect, [ length( List ), [], self() ] ),
lists:foldl(
fun( X, Indx ) ->
spawn_link( my_pmap, map, [ F, X, Indx, CollectorPid ] ),
Indx + 1
end,
1,
List ),
Mapped =
receive
{ collected, M } ->
M
end,
Sorted = lists:sort(
fun( { Indx1, _ }, { Indx2, _ } ) ->
Indx1 < Indx2
end,
Mapped ),
[ Val || { _Indx, Val } <- Sorted ].
collect( 0, List, SenderPid ) ->
SenderPid ! { collected, List };
collect( N, List, SenderPid ) when N > 0 ->
receive
Mapped ->
collect( N - 1, [ Mapped | List ], SenderPid )
end.
这是测试结果:
1> c(my_pmap).
{ok,my_pmap}
2> timer:tc( my_pmap, pmap, [ fun(X) -> X*X*X*X end, lists:seq( 1, 10000 ) ] ).
{137804,
[1,16,81,256,625,1296,2401,4096,6561,10000,14641,20736,
28561,38416,50625,65536,83521,104976,130321,160000,194481,
234256,279841,331776,390625,456976,531441|...]}
3> timer:tc( lists, map, [ fun(X) -> X*X*X*X end, lists:seq( 1, 10000 ) ] ).
{44136,
[1,16,81,256,625,1296,2401,4096,6561,10000,14641,20736,
28561,38416,50625,65536,83521,104976,130321,160000,194481,
234256,279841,331776,390625,456976,531441|...]}
正如您可能已经看到的 0,137804 秒。与0,044136 秒相比。
谢谢