1, foreword last night in the Erlang Technical Exchange Group asked such a question:
I now have more than 10,000 static key-value data, and I am now directly generating the following code to use:
Get (Key1)->value1;
Get (Key2)->value2;
......
Get (KeyN)->valuen.
Problem:
1. Is there a more efficient way?
2. What is the algorithm for the matching of Erlang?
3. Where can you see the implementation principle of function matching in the source code?
It was suggested that ETS should be used, so a simple test was done on this.
2. Test code 3, run test
The same key-value pairs of data are written to the ETS and Erl files, respectively,
The test results for the same 1000w execution of different value data types are as follows:
% value type: tuple ()
% Total Data: 1w
% Test2:run (10000000).
% "Get" [total:546 (546) Ms avg:0.055 (0.055) US]
% "ETS" [total:2340 (2480) Ms avg:0.234 (0.248) US]
% ========================================================================
% get = 546.00ms [100.00%] 546.00ms [100%]
% ETS = 2340.00ms [428.57%] 2480.00ms [454.21%]
% ========================================================================
% value type: tuple ()
% Total Data: 10w
% Test2:run (10000000).
% "Get" [total:515 (515) Ms avg:0.051 (0.051) US]
% "ETS" [total:3588 (3604) Ms avg:0.359 (0.360) US]
% ========================================================================
% get = 515.00ms [100.00%] 515.00ms [100%]
% ETS = 3588.00ms [696.70%] 3604.00ms [699.81%]
% ========================================================================
% value type: binary ()
% Total Data: 1w
% Test2:run (10000000).
% "Get" [total:515 (515) Ms avg:0.051 (0.051) US]
% "ETS" [total:2465 (2464) Ms avg:0.247 (0.246) US]
% ========================================================================
% get = 515.00ms [100.00%] 515.00ms [100%]
% ETS = 2465.00ms [478.64%] 2464.00ms [478.45%]
% ========================================================================
% value type: binary ()
% Total Data: 10w
%
% Kvs.erl file is 8M, I cannot compile on my machine (process memory allocation is up to maximum)
% value type: integer ()
% Total Data: 10w
% Test2:run (10000000).
% "Get" [total:499 (515) Ms avg:0.050 (0.051) US]
% "ETS" [total:3307 (3307) Ms avg:0.331 (0.331) US]
% ========================================================================
% get = 499.00ms [100.00%] 515.00ms [100%]
% ETS = 3307.00ms [662.73%] 3307.00ms [642.14%]
% ========================================================================
% value type: integer () (greater than 32 bits)
% Total Data: 10w
% Test2:run (10000000).
% "Get" [total:546 (546) Ms avg:0.055 (0.055) US]
% "ETS" [total:3744 (3837) Ms avg:0.374 (0.384) US]
% ========================================================================
% get = 546.00ms [100.00%] 546.00ms [100%]
% ETS = 3744.00ms [685.71%] 3837.00ms [702.75%]
% ========================================================================
% value type: term () (composite structure)
% Total Data: 1w
% Test2:run (10000000).
% "Get" [total:530 (531) Ms avg:0.053 (0.053) US]
% "ETS" [total:3089 (3447) Ms avg:0.309 (0.345) US]
% ========================================================================
% get = 530.00ms [100.00%] 531.00ms [100%]
% ETS = 3089.00ms [582.83%] 3447.00ms [649.15%]
% ========================================================================
4, test results (1), static key value pairs (Key-value) data directly written as Erl file compiled after reading than the ETS table 6 times times faster;
(2), the larger the data size, the more complex data structure, the difference between the more obvious;
(3), the Erl file is too large (greater than 10M), the compilation will be very slow (more than 1 minutes);
(4), the amount of data in about 1w, written in the Erl file 3M, it is advisable to use the Erl file form faster and more direct.
PS: My test does not guarantee fairness, but also look to the big God to raise points.
This article comes from the Erlang development blog
Http://blog.csdn.net/zhongruixian
Original address: http://blog.csdn.net/zhongruixian/article/details/44885953
Erlang static Key-value pair (Key-value) data is written separately to the Erl file and the ETS table read test