某頁遊erlang服務端廣播演算法效率好差,應該算是一個bug了吧,erlang服務端
偶然得到一份erlang網頁服務端的代碼
寫的非常優雅,文檔也非常不錯,但我看到他的情境管理中的廣播演算法,寫的很漂亮,但效率非常低。
每次都全域遍曆全部線上玩家在某個情境的玩具ID,這樣的演算法,我不知道一個服能撐起多少人,最佳化一下,情境服務管理在他服務中的玩家,NPC,MON,效率會提升好幾倍,只是代碼會稍微複雜一點,沒他寫的優雅了!!
AllUser = ets:match(?ETS_ONLINE, #ets_online{id = '$1',x = '$2', y='$3', scene = Q, _='_'}),<span style="color:#ff6666;"> %% 這句代碼效率會很差的,而去執行的次數又多</span>
%% 擷取要廣播的範圍使用者IDget_broadcast_id(Q, X0, Y0) -> AllUser = ets:match(?ETS_ONLINE, #ets_online{id = '$1',x = '$2', y='$3', scene = Q, _='_'}),<span style="color:#ff6666;"> %% 這句代碼效率會很差的,而去執行的次數又多</span> XY2 = lib_scene:get_xy(X0, Y0), get_broadcast_id_loop(AllUser, XY2, []).get_broadcast_id_loop([], _XY2, D) -> D;get_broadcast_id_loop([[Id, X, Y]|T], XY2, D) -> XY = lib_scene:get_xy(X, Y), if XY == XY2 orelse XY == XY2 + 1 orelse XY == XY2 -1 orelse XY == XY2 -8 orelse XY == XY2 +8 orelse XY == XY2 -9 orelse XY == XY2 +9 orelse XY == XY2 -7 orelse XY == XY2+7 -> get_broadcast_id_loop(T, XY2, D++[Id]); true-> get_broadcast_id_loop(T, XY2, D) end.