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
[url]http://www.erlang.org/doc/efficiency_guide/myths.html#id61433[/url]
[quote]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.[/quote]

[url]http://www.cnblogs.com/me-sa/archive/2012/05/06/erlang-function-call-efficiency.html[/url]
[img]http://dl2.iteye.com/upload/attachment/0088/2826/8d59f69b-7020-3826-a48b-d13766014370.png[/img]


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值