/jaerlang-code/code/benchmark_assoc.erl

https://github.com/killme2008/erlib · Erlang · 167 lines · 119 code · 23 blank · 25 comment · 0 complexity · 3038b3488165695527a1114ebeb9d804 MD5 · raw file

  1. %% ---
  2. %% Excerpted from "Programming Erlang",
  3. %% published by The Pragmatic Bookshelf.
  4. %% Copyrights apply to this code. It may not be used to create training material,
  5. %% courses, books, articles, and the like. Contact us if you are in doubt.
  6. %% We make no guarantees that this code is fit for any purpose.
  7. %% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
  8. %%---
  9. -module(benchmark_assoc).
  10. -compile(export_all).
  11. -import(lib_misc, [unconsult/2]).
  12. %% make a random list of [{<<Bin>>, Int}] length N
  13. test1(K) when K < 200000->
  14. L = make_assoc_list(K),
  15. Mid = K div 2,
  16. {Key,_} = lists:nth(Mid, L),
  17. %% create the file
  18. File = "big.tmp",
  19. lib_misc:unconsult("big.tmp", L),
  20. Size = filelib:file_size(File),
  21. %% time the first update method
  22. {T1, _} = timer:tc(?MODULE, f1, [Key, File]),
  23. %% clean up
  24. file:delete(File),
  25. {consult, Size, T1};
  26. test1(_) ->
  27. no.
  28. test2(K) when K < 300000->
  29. L = make_assoc_list(K),
  30. Mid = K div 2,
  31. {Key,_} = lists:nth(Mid, L),
  32. %% create the file
  33. File = "big.tmp",
  34. file:write_file(File, [term_to_binary(L)]),
  35. Size = filelib:file_size(File),
  36. {T, _} = timer:tc(?MODULE, f2, [Key, File]),
  37. %% clean up
  38. file:delete(File),
  39. {binary, Size, T};
  40. test2(_) ->
  41. no.
  42. test3(K) ->
  43. L = make_assoc_list(K),
  44. %% io:format("L=~p~n",[L]),
  45. Mid = K div 2,
  46. {Key,_} = lists:nth(Mid, L),
  47. File = "big.dets",
  48. Big = ets:new(big, []),
  49. lists:foreach(fun(KV) -> ets:insert(Big, KV) end, L),
  50. %% Now iopen a dets table
  51. io:format("made ets~n"),
  52. _V = ets:foldr(fun(X,A) -> [X|A] end, [], Big),
  53. %% io:format("V=~p~n",[V]),
  54. {ok, newd} = dets:open_file(newd, [{file,File}]),
  55. ets:to_dets(Big, newd),
  56. _V1 = dets:foldr(fun(X,A) -> [X|A] end, [], newd),
  57. %% io:format("V1=~p~n",[V1]),
  58. ets:delete(Big),
  59. dets:close(newd),
  60. Size = filelib:file_size(File),
  61. {T, _} = timer:tc(?MODULE, f3, [Key, File]),
  62. %% clean up
  63. file:delete(File),
  64. {dets, Size, T}.
  65. f1(Key, File) ->
  66. update(File, Key, 20).
  67. f2(Key, File) ->
  68. update1(File, Key, 20).
  69. f3(Key, File) ->
  70. {ok, big} = dets:open_file(big, [{file,File}]),
  71. %% V1 = dets:foldr(fun(X,A) -> [X|A] end, [], big),
  72. %% io:format("V1=~p~n",[V1]),
  73. %% io:format("Key=~p~n",[Key]),
  74. case dets:lookup(big, Key) of
  75. [{Key,Val}] ->
  76. %% io:format("oldval=~p~n",[Val]),
  77. dets:insert(big, {Key, Val+20});
  78. LL ->
  79. io:format("*** error ***:~p~n",[LL])
  80. end,
  81. dets:close(big).
  82. make_assoc_list(N) ->
  83. random_seed(),
  84. make_random_list(N, []).
  85. %% goes like N^2
  86. make_random_list(0, L) -> L;
  87. make_random_list(N, L) ->
  88. %% Key = list_to_binary(random_string(4,6,$a, 26)),
  89. Key = (random_string(4,6,$a, 26)),
  90. case is_defined(Key, L) of
  91. true ->
  92. make_random_list(N , L);
  93. false ->
  94. Val = list_to_integer(random_string(2,6,$0,10)),
  95. make_random_list(N-1, [{Key,Val}|L])
  96. end.
  97. is_defined(Key, [{Key,_}|_]) -> true;
  98. is_defined(Key, [_|T]) -> is_defined(Key, T);
  99. is_defined(_, []) -> false.
  100. random_string(Min, Extra, Start, K) ->
  101. Length = Min + random:uniform(Extra),
  102. random_str(Length, Start, K, []).
  103. random_str(0, _, _, D) ->
  104. D;
  105. random_str(N, Start, K, D) ->
  106. random_str(N-1, Start, K, [random:uniform(K)+ Start - 1|D]).
  107. random_seed() ->
  108. {_,_,X} = erlang:now(),
  109. {H,M,S} = time(),
  110. H1 = H * X rem 32767,
  111. M1 = M * X rem 32767,
  112. S1 = S * X rem 32767,
  113. put(random_seed, {H1,M1,S1}).
  114. update1(File, Key, Delta) ->
  115. {ok, Bin} = file:read_file(File),
  116. Terms = binary_to_term(Bin),
  117. Terms1 = do_update(Key, Delta, Terms),
  118. file:write_file(File, [term_to_binary(Terms1)]).
  119. update(File, Key, Delta) ->
  120. {ok, Terms} = file:consult(File),
  121. Terms1 = do_update(Key, Delta, Terms),
  122. unconsult(File, Terms1).
  123. do_update(Key, Delta, [{Key,Val}|T]) ->
  124. [{Key,Val+Delta}|T];
  125. do_update(Key, Delta, [H|T]) ->
  126. [H|do_update(Key, Delta, T)];
  127. do_update(Key, Delta, []) ->
  128. [{Key, Delta}].
  129. yes() ->
  130. {ok, abc} = dets:open_file(abc, [{file,"foo.dets"}]),
  131. L = [{<<"abc">>,123},{<<"zrt123">>,3456},{<<"trui67f">>,22345}],
  132. lists:foreach(fun(I) -> dets:insert(abc, I) end, L),
  133. dets:close(abc),
  134. {ok, abc} = dets:open_file(abc, [{file,"foo.dets"}]),
  135. Val = dets:lookup(abc, <<"zrt123">>),
  136. dets:close(abc),
  137. Val.
  138. yes1() ->
  139. Tab = ets:new(abc, []),
  140. L = [{<<"abc">>,123},{<<"zrt123">>,3456},{<<"trui67f">>,22345}],
  141. lists:foreach(fun(I) -> ets:insert(Tab, I) end, L),
  142. V1 = ets:lookup(Tab, <<"zrt123">>),
  143. io:format("V1=~p~n",[V1]),
  144. ets:delete(Tab).