关键字: process dict get put
一直以来 erlang的几本书的作者都建议不要用process dict,倒不是它的性能不好,而是因为process dict破坏了fp的变量不可变语义,所以引起了不好的印象。其实在很多场合,是很合用的。process dict 有几个好处:
1. 无锁,所以高速。
2. hash实现。
3. 内容参与gc。
4. 实现的很细致。
5. 变量可变。
以下是试验:
root@nd-desktop:~# cat dicttest.erl
-module(dicttest).
-export([test_put/1, test_get/1]).
test_put(N)->
Start = erlang:now(),
dotimes(N, fun (I) -> put(I, hello) end),
Stop = erlang:now(),
N / time_diff(Start, Stop).
test_get(N)->
Start = erlang:now(),
dotimes(N, fun (I) -> get(I) end),
Stop = erlang:now(),
N / time_diff(Start, Stop).
dotimes(0, _) -> done;
dotimes(N, F) ->
F(N),
dotimes(N - 1, F).
time_diff({A1,A2,A3}, {B1,B2,B3}) ->
(B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 .
root@nd-desktop:~# erl -smp disable +h 9999999
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.2 (abort with ^G)
1> dicttest:test_put(1000000).
9174480.26569295
2> dicttest:test_get(1000000).
34172105.390379503
3> length(element(2, process_info(self(), dictionary))).
1000000
测试的速度相当的快了。 百万条级别,插入100ns, 查询40ns. 而ets的速度大概是us,差了一个数量级别。
结论:合适的场合 猛用。
转载: http://mryufeng.iteye.com/blog/435642
1. 无锁,所以高速。
2. hash实现。
3. 内容参与gc。
4. 实现的很细致。
5. 变量可变。
以下是试验:
root@nd-desktop:~# cat dicttest.erl
-module(dicttest).
-export([test_put/1, test_get/1]).
test_put(N)->
Start = erlang:now(),
dotimes(N, fun (I) -> put(I, hello) end),
Stop = erlang:now(),
N / time_diff(Start, Stop).
test_get(N)->
Start = erlang:now(),
dotimes(N, fun (I) -> get(I) end),
Stop = erlang:now(),
N / time_diff(Start, Stop).
dotimes(0, _) -> done;
dotimes(N, F) ->
F(N),
dotimes(N - 1, F).
time_diff({A1,A2,A3}, {B1,B2,B3}) ->
(B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 .
root@nd-desktop:~# erl -smp disable +h 9999999
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.2 (abort with ^G)
1> dicttest:test_put(1000000).
9174480.26569295
2> dicttest:test_get(1000000).
34172105.390379503
3> length(element(2, process_info(self(), dictionary))).
1000000
测试的速度相当的快了。 百万条级别,插入100ns, 查询40ns. 而ets的速度大概是us,差了一个数量级别。
结论:合适的场合 猛用。
转载: http://mryufeng.iteye.com/blog/435642
erlang牛人的博客
3763

被折叠的 条评论
为什么被折叠?



