Created
February 13, 2012 15:59
-
-
Save kduman/1817839 to your computer and use it in GitHub Desktop.
Dict & List speed test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %% Author: Said | |
| %% Created: 13.02.2012 | |
| -module(test). | |
| %% | |
| %% Include files | |
| %% | |
| %% | |
| %% Exported Functions | |
| %% | |
| -export([start/1, speed_test/0]). | |
| %% | |
| %% API Functions | |
| %% | |
| start(Test) -> | |
| case Test of | |
| dict -> dict_test(); | |
| list -> list_test(); | |
| _ -> {error, test_not_defined} | |
| end. | |
| speed_test() -> | |
| {List, _} = timer:tc(fun list_test/0), | |
| {Dict, _} = timer:tc(fun dict_test/0), | |
| MakeSec = fun(MicroSec) -> | |
| lists:concat(["~", round(MicroSec / 1000 / 1000), " sec."]) | |
| end, | |
| {total, | |
| {{list_time, MakeSec(List)}, | |
| {dict_time, MakeSec(Dict)}}, | |
| {ratio, lists:concat([round(Dict / List * 100), "%"])}}. | |
| %% | |
| %% Local Functions | |
| %% | |
| % Keyfind (from jobcheck_utils) | |
| keyfind(Key, List) -> | |
| case keyfind(Key, 1, List) of | |
| undefined -> undefined; | |
| {_, Value} -> Value | |
| end. | |
| keyfind(Key, N, List) -> | |
| case lists:keyfind(Key, N, List) of | |
| false -> undefined; | |
| Value -> Value | |
| end. | |
| keystore({Key, _} = NewTuple, List) -> | |
| lists:keystore(Key, 1, List, NewTuple). | |
| % tests | |
| dict_test() -> | |
| Data = [dict:from_list(DL) || DL <- test_data()], | |
| Test = | |
| fun(D) -> | |
| dict:find(id, D), | |
| dict:find(login, D), | |
| dict:find(password, D), | |
| dict:find(first_name, D), | |
| dict:find(last_name, D), | |
| dict:find(middle_name, D), | |
| D1 = dict:store(password, <<"321">>, D), | |
| D2 = dict:store(first_name, <<"Iliya">>, D1), | |
| dict:store(middle_name, <<"S">>, D2) | |
| end, | |
| lists:foreach(Test, Data). | |
| list_test() -> | |
| Data = test_data(), | |
| Test = | |
| fun(D) -> | |
| keyfind(id, D), | |
| keyfind(login, D), | |
| keyfind(password, D), | |
| keyfind(first_name, D), | |
| keyfind(last_name, D), | |
| keyfind(middle_name, D), | |
| D1 = keystore({password, <<"321">>}, D), | |
| D2 = keystore({first_name, <<"Iliya">>}, D1), | |
| keystore({middle_name, <<"S">>}, D2) | |
| end, | |
| lists:foreach(Test, Data). | |
| % other | |
| test_data() -> | |
| lists:duplicate(1000000, % 1.000.000 | |
| [{id, 1}, | |
| {login, <<"Deepro">>}, | |
| {password, <<"123">>}, | |
| {first_name, <<"Kostya">>}, | |
| {last_name, <<"Duman">>}, | |
| {middle_name, <<"V">>}]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment