Erlang etsテーブル関連
1992 ワード
%%% @copyright (C) 2016,
%%% @doc
%%%
%%% @end
%%% Created : 02. 2016 3:07
%%%-------------------------------------------------------------------
-module(test).
-author("mohe").
-include_lib("stdlib/include/ms_transform.hrl").
-record(user, {
id :: integer(),
name :: string()
}).
%% API
-compile([export_all/1]).
-define(ETS_NAME, ets_test).
init_ets({N}) ->
ets:new(?ETS_NAME, [named_table, public, set, {keypos, #user.id},
{write_concurrency, true}, {read_concurrency, true}]),
lists:foreach(fun(X) ->
ets:insert(?ETS_NAME, #user{id = X, name = random_str()}) end,
lists:seq(1, N)).
%% id N
gt_id(N) ->
T1 = ms_time(),
Mspec = ets:fun2ms(fun(#user{id = Id} = X) when Id > N -> X end),
Res = ets:select(?ETS_NAME, Mspec),
T2 = ms_time(),
io:format("cost:~p ms", [T2 - T1]),
Res.
ms_time() ->
{_, S, M} = os:timestamp(), (S * 1000000 + M) div 1000.
%%
random_str() -> Str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_",
%% ,
N = [random:uniform(length(Str))|| _Elem
テスト
test:init_ets({10000}).
cost:20 ms...
その他