本文主要是介绍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进程
这篇关于erlang ets heir read_concurrency的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!