Created
August 20, 2008 02:39
-
-
Save cooldaemon/6312 to your computer and use it in GitHub Desktop.
lists module vs func vs list comprehension.
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
-module(lists_vs_func_vs_comp). | |
-author('[email protected]'). | |
-export([test/0]). | |
test() -> | |
Seqs = lists:seq(0, 500000), | |
lists:foreach( | |
fun | |
({F, TargetName}) -> | |
{_Result, [RunTime, WallClock]} = benchmark(fun () -> F(Seqs) end), | |
io:fwrite( | |
"~s:~p(~p)ms~n", | |
[TargetName, RunTime, WallClock] | |
); | |
(Message) -> | |
io:fwrite("--<~s>--~n", [Message]) | |
end, | |
[ | |
"foreach", | |
{fun lists_foreach_anon/1, "lists(anonymous function)"}, | |
{fun lists_foreach/1, "lists"}, | |
{fun func_foreach_tailrec/1, "func(tail recursive)"}, | |
{fun func_foreach/1, "func"}, | |
"reverse", | |
{fun lists_reverse/1, "lists"}, | |
{fun func_reverse/1, "func"}, | |
"map", | |
{fun lists_map/1, "lists"}, | |
{fun func_map/1, "func"}, | |
{fun func_map_without_reverse/1, "func(without reverse)"}, | |
{fun comp_map/1, "comp"}, | |
"foldl", | |
{fun lists_foldl/1, "lists"}, | |
{fun func_foldl/1, "func"}, | |
"filter", | |
{fun lists_filter/1, "lists"}, | |
{fun func_filter/1, "func"}, | |
{fun comp_filter/1, "comp"}, | |
"map and filter", | |
{fun lists_map_filter/1, "lists"}, | |
{fun func_map_filter/1, "func"}, | |
{fun comp_map_filter/1, "comp"} | |
] | |
). | |
lists_foreach_anon(Seqs) -> | |
lists:foreach(fun (N) -> N * N * N end, Seqs). | |
lists_foreach(Seqs) -> | |
lists:foreach(fun foreach/1, Seqs). | |
foreach(N) -> N * N * N. | |
func_foreach_tailrec([N]) -> N * N * N; | |
func_foreach_tailrec([N | Ns]) -> N * N * N, func_foreach_tailrec(Ns). | |
func_foreach([N]) -> N * N * N; | |
func_foreach([N | Ns]) -> func_foreach_tailrec(Ns), N * N * N. | |
lists_reverse(Seqs) -> | |
lists:reverse(Seqs). | |
func_reverse(Seqs) -> func_reverse(Seqs, []). | |
func_reverse([], NewList) -> NewList; | |
func_reverse([N | Ns], NewList) -> func_reverse(Ns, [N | NewList]). | |
lists_map(Seqs) -> | |
lists:map(fun (N) -> N * N end, Seqs). | |
func_map(Seqs) -> func_map(Seqs, []). | |
func_map([], NewList) -> lists:reverse(NewList); | |
func_map([N | Ns], NewList) -> func_map(Ns, [N * N | NewList]). | |
func_map_without_reverse(Seqs) -> func_map_without_reverse(Seqs, []). | |
func_map_without_reverse([], NewList) -> NewList; | |
func_map_without_reverse([N | Ns], NewList) -> | |
func_map_without_reverse(Ns, [N * N | NewList]). | |
comp_map(Seqs) -> | |
[N * N || N <- Seqs]. | |
lists_foldl(Seqs) -> | |
lists:foldl(fun (N, Sum) -> N + Sum end, 0, Seqs). | |
func_foldl(Seqs) -> func_foldl(Seqs, 0). | |
func_foldl([], Sum) -> Sum; | |
func_foldl([N | Ns], Sum) -> func_foldl(Ns, N + Sum). | |
lists_filter(Seqs) -> | |
lists:filter( | |
fun | |
(N) when N rem 2 =:= 0 -> true; | |
(_N) -> false | |
end, | |
Seqs | |
). | |
func_filter(Seqs) -> func_filter(Seqs, []). | |
func_filter([], NewList) -> lists:reverse(NewList); | |
func_filter([N | Ns], NewList) when N rem 2 =:= 0 -> | |
func_filter(Ns, [N | NewList]); | |
func_filter([_N | Ns], NewList) -> | |
func_filter(Ns, NewList). | |
comp_filter(Seqs) -> | |
[N || N <- Seqs, N rem 2 =:= 0]. | |
lists_map_filter(Seqs) -> | |
lists:map( | |
fun (N) -> N * N end, | |
lists:filter( | |
fun | |
(N) when N rem 2 =:= 0 -> true; | |
(_N) -> false | |
end, | |
Seqs | |
) | |
). | |
func_map_filter(Seqs) -> func_map_filter(Seqs, []). | |
func_map_filter([], NewList) -> lists:reverse(NewList); | |
func_map_filter([N | Ns], NewList) when N rem 2 =:= 0 -> | |
func_map_filter(Ns, [N * N | NewList]); | |
func_map_filter([_N | Ns], NewList) -> | |
func_map_filter(Ns, NewList). | |
comp_map_filter(Seqs) -> | |
[N * N || N <- Seqs, N rem 2 =:= 0]. | |
benchmark(TargetFunction) -> | |
lists:foreach(fun statistics/1, [runtime, wall_clock]), | |
Result = TargetFunction(), | |
Times = lists:map( | |
fun (Type) -> {_, T} = statistics(Type), T end, | |
[runtime, wall_clock] | |
), | |
{Result, Times}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment