`
zhongwencool
  • 浏览: 27028 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

fun还是如以前一样狂跩吊么?

阅读更多
fun这么好用。为什么老大在最近都说不要用?

gen:call(PID,{func,fun() ->broadcast_to_whole_map(Record) end}).

handle_call({func,F},_From,State) ->
   Reply =  F(),
{reply, Reply, State}.


gen:call(PID,{server_module,broadcast_to_whole_map,[Record]}).

handle_call({server_module,broadcast_to_whole_map,[Record]},_From,State) ->
Reply = server_module:broadcast_to_whole_map(Record) ,
{reply,Reply,State}.

这2个有什么区别?为什么要改?

fun() ->end
erlang efficiency guide第一个原则就是说:

本地调用>=跨模块调用> fun() ->apply/3
http://www.erlang.org/doc/efficiency_guide/myths.html#id61433
引用
2.1 Myth: Funs are slowYes, funs used to be slow. Very slow. Slower than apply/3. Originally, funs were implemented using nothing more than compiler trickery, ordinary tuples, apply/3, and a great deal of ingenuity.
But that is ancient history. Funs was given its own data type in the R6B release and was further optimized in the R7B release. Now the cost for a fun call falls roughly between the cost for a call to local function and apply/3.


http://www.cnblogs.com/me-sa/archive/2012/05/06/erlang-function-call-efficiency.html



当然由于版本的更新,效率差距估计不会这么大。但是
本地调用>=跨模块调用> fun() ->apply/3 这个是确定的。
所以最近游戏在优化广播发包时把广播由fun() ->end 改为Mod:Fun(Arg)是非常好一种尝试。
  • 大小: 52.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics