erlang ets heir read_concurrency

ets  在创建的时候有这个一个选项  read_concurrency 

官方文档解释如下


{read_concurrency,boolean()} Performance tuning. 
Default is false. When set to true, the table is optimized for concurrent read operations. 
When this option is enabled on a runtime system with SMP support, read operations become much cheaper; 
especially on systems with multiple physical processors.However, switching between read and write operations becomes more expensive. 

You typically want to enable this option when concurrent read operations are much more frequent than write operations, or when concurrent 
reads and writes comes in large read and write bursts (i.e., lots of reads not interrupted by writes,and lots of writes not interrupted by reads). 
You typically do not want to enable this option when the common access pattern is a few read operations interleaved with a few write operations repeatedly. 
In this case you will get a performance degradation by enabling this option. The read_concurrency option can be combined with the write_concurrency option. 
You typically want to combine these when large concurrent read bursts and large concurrent write bursts are common.


大致意思是,开启这个属性后 并发读效率提升,但是读/写之间切换效率会降低。

当有连续读需求,并且中间不穿插写操作时,可以试一下开启这个功能


做了一个小测试


%%首先启动三个ets log_time用于记录每个查询操作消耗的时间
ets:new(open_concurrency, [set, public, named_table, {keypos, 1}, {read_concurrency,true}].
ets:new(close_concurrency, [set, public, named_table, {keypos, 1}, {read_concurrency,false}].
ets:new(log_time, [set, public, named_table, {keypos, 1}, {read_concurrency,true}].

read(EtsName) ->
    %%选择一个ets表先写入数据用于被查询
    FunUpdate = 
              fun(Nth) ->
                       cache:update(EtsName, Nth, Nth)
              end,
    lists:foreach(FunUpdate, lists:sql(1, 10000)),
    ets:delete_all_objects(log_time),

    FunGetTime = 
               fun(Nth) -> 
                   spawn(fun() ->
                         SelectNth = utils:rand(1, 10000),
                        {SelectTime, _} = timer:tc(fun() ->
                                          cache:get_value(EtsName, SelectNth)
                                                   end),
                         %%由于在子进程内,只能将时间数据储存在ets中
                         cache:update(log_time, Nth, SelectTime)
                         end)
                end,
    lists:froeach(FunPrint, lists:seq(1, 1000000)).
    %%随后再从log_time中将时间相加
 

 
 

对开启并发读/不开启并发读的ets分别 做了100万次的读取,

在windows上测试了下结果,发现不开启并发读的时间竟然每次都少。。。

尴尬有时间了再去linux环境下看看。。


ets  heir 当前ets 进程销毁后,会将数据发送至设定的heir进程


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值