Erlang:ets选择并匹配性能 [英] Erlang: ets select and match performance

查看:346
本文介绍了Erlang:ets选择并匹配性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现函数ets:select / 2和mnesia:select / 3优于ets:match / 2,ets:match_object / 2和mnesia:match_object / 3 引用链接: http://www.erlang.org/doc/efficiency_guide/tablesDatabases.html

我读了一些有关选择和匹配之间进行比较的文章,得出结论,有一些因素会影响结果,例如表中的记录数量,选择/匹配主键与否,表类型(袋子,设置...)等。

And I'd read some essay about comparing between select and match, I conclude there are some factor effecting the result, such as records' amount in table, select/match a primary key or not, table kind(bag, set...), etc.

在我的测试中,我对所有功率为10W的表都适用记录和1W记录,并且只能选择/匹配非主键。

In my test, I do for all kind of table with 10W records and 1W records, and only select/match for a un-primary key.

以下代码:

select_ets_test(Times) ->
    MS = ets:fun2ms(fun(T) when T#ets_haoxian_template.count == 15 -> T end),
    T1 = timer:tc(?MODULE, todo, [fun() -> ets:select(haoxian_test_bag, MS) end, Times]),
    T2 = timer:tc(?MODULE, todo, [fun() -> ets:select(haoxian_test_set, MS) end, Times]),
    T3 = timer:tc(?MODULE, todo, [fun() -> ets:select(haoxian_test_ordered_set, MS) end, Times]),
    T4 = timer:tc(?MODULE, todo, [fun() -> ets:select(haoxian_test_duplicate_bag, MS) end, Times]),
    io:format("select bag           : ~p~n", [T1]),
    io:format("select set           : ~p~n", [T2]),
    io:format("select ordered_set   : ~p~n", [T3]),
    io:format("select duplicate bag : ~p~n", [T4]).

match_ets_test(Times) ->
    MS = #ets_haoxian_template{count = 15, _ = '_' },
    T1 = timer:tc(?MODULE, todo, [fun() -> ets:match_object(haoxian_test_bag, MS) end, Times]),
    T2 = timer:tc(?MODULE, todo, [fun() -> ets:match_object(haoxian_test_set, MS) end, Times]),
    T3 = timer:tc(?MODULE, todo, [fun() -> ets:match_object(haoxian_test_ordered_set, MS) end, Times]),
    T4 = timer:tc(?MODULE, todo, [fun() -> ets:match_object(haoxian_test_duplicate_bag, MS) end, Times]),
    io:format("match bag           : ~p~n", [T1]),
    io:format("match set           : ~p~n", [T2]),
    io:format("match ordered_set   : ~p~n", [T3]),
    io:format("match duplicate bag : ~p~n", [T4]).

todo(_Fun, 0) ->
    ok;
todo(Fun, Times) ->
    Fun(),
    todo(Fun, Times - 1).

记录如下:#ets_haoxian_template {type = X,count = Y,...},关键姿势是类型。

the record would like: #ets_haoxian_template{type = X, count = Y, ...}, keypose is type.

以下结果:
1W测试:

the resule following: 1W test:

insert bag           : {324000,true}
insert set           : {221000,true}
insert ordered_set   : {108000,true}
insert duplicate bag : {173000,true}

select bag           : {284000,ok}
select set           : {255000,ok}
select ordered_set   : {221000,ok}
select duplicate bag : {252000,ok}

match bag           : {238000,ok}
match set           : {192000,ok}
match ordered_set   : {136000,ok}
match duplicate bag : {191000,ok}

10W测试:

insert bag           : {1654000,true}
insert set           : {1684000,true}
insert ordered_set   : {981000,true}
insert duplicate bag : {1769000,true}

select bag           : {3404000,ok}
select set           : {3433000,ok}
select ordered_set   : {2501000,ok}
select duplicate bag : {3678000,ok}

match bag           : {2749000,ok}
match set           : {2927000,ok}
match ordered_set   : {1748000,ok}
match duplicate bag : {2923000,ok}

似乎匹配比选择更好?还是我的测试有问题?

It seem match is better than select? Or my test something wrong???

推荐答案

match 函数使用特殊的元组语法( match_pattern )决定返回什么。

The match function employs a special tuple syntax (match_pattern) to decide what to return.

select 函数采用特殊的元组语法(< a href = http://erlang.org/doc/man/ets.html#type-match_spec rel = nofollow noreferrer> match_spec )这是 match_pattern 的超集,能够指定后卫并从结果集中提取元素(而不仅仅是返回匹配的键)。

The select function employs a special tuple syntax (match_spec) that is a superset of match_pattern, with the ability to specify guards and extract elements from the result set (rather than just returning the matching keys).

我的理解是:


  • select 会编译 match_spec 转换为匿名函数,从而加快了运行速度

  • 为该函数提供防护的能力可以比以前更快地消除误报只需 match_pattern (si (因为它们将首先运行)

  • 就地从结果集中提取元素的功能可以节省您以后要做的工作,而不是遍历返回的键来提取该数据

  • select compiles the match_spec into an anonymous function, expediting how fast it runs
  • the ability to provide guards to this function eliminates false positives quicker than is possible with just a match_pattern (since they will run first)
  • the ability to extract elements from the result set in-place saves you work you would have to do later, rather than iterating over the returned keys to extract that data.

在琐碎的非特定用例中, select 只是一个匹配的很多工作。在非平凡的更常见的用例中, select 会给您您真正想要的更快。

In trivial non-specific use-cases, select is just a lot of work around match. In non-trivial more common use-cases, select will give you what you really want a lot quicker.

这篇关于Erlang:ets选择并匹配性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆