Skip to content

Instantly share code, notes, and snippets.

@kellymclaughlin
Created August 13, 2014 18:44
Show Gist options
  • Save kellymclaughlin/3768652d9af269c45897 to your computer and use it in GitHub Desktop.
Save kellymclaughlin/3768652d9af269c45897 to your computer and use it in GitHub Desktop.
Comparison of aggregating a sorted list in Erlang
Erlang R16B02-basho5 (erts-5.10.3) [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]
Eshell V5.10.3 (abort with ^G)
1> sort_test:run().
Sort once at the end: 10266
Sort after each append: 349
ok
2> sort_test:run().
Sort once at the end: 9724
Sort after each append: 431
ok
3> sort_test:run().
Sort once at the end: 9407
Sort after each append: 435
ok
4> sort_test:run().
Sort once at the end: 9740
Sort after each append: 609
ok
-module(sort_test).
-compile(export_all).
run() ->
Lists = [[{druuid:v4(), X} || X <- lists:seq(1, 30)] || _ <- lists:seq(1, 200)],
{Res1, _} = timer:tc(ordsets, from_list, [lists:append(Lists)]),
{Res2, _} = timer:tc(ordsets, from_list, [append_and_sort(Lists)]),
io:format("Sort once at the end: ~p~n Sort after each append: ~p~n", [Res1, Res2]),
ok.
append_and_sort(Lists) ->
FoldFun =
fun(L, Acc) ->
lists:usort(Acc ++ L)
end,
lists:foldl(FoldFun, [], Lists).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment