标签:
偶然得到一份erlang网页服务端的代码每次都全局遍历全部在线玩家在某个场景的玩具ID,这样的算法,我不知道一个服能撑起多少人,优化一下,场景服务管理在他服务中的玩家,NPC,MON,效率会提升好几倍,只是代码会稍微复杂一点,没他写的优雅了!!
%% 获取要广播的范围用户ID get_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.
某页游erlang服务端广播算法效率好差,应该算是一个bug了吧
标签:
原文地址:http://blog.csdn.net/gamesofsailing/article/details/42538233