Tail/Non tail recursion
WILLIAMS Dominic
D.WILLIAMS@REDACTED
Thu Aug 28 15:56:05 CEST 2003
-module(square).
-export([run/0,plain/1,map/1,comprehension/1]).
run() ->
List = lists:seq(1,1000000),
timer:start(),
lists:foreach(
fun(Fun) ->
{Time,_} = timer:tc(?MODULE,Fun,[List]),
io:fwrite("~w: ~w~n",[Fun,Time])
end,
[plain,map,comprehension]).
plain([H|T]) -> [H*H|plain(T)];
plain([]) -> [].
map(Numbers) ->
lists:map(fun(X)->X*X end,Numbers).
comprehension(Numbers)->
[X*X||X<-Numbers].
------
2> c(square).
{ok,square}
3> square:run().
plain: 3155000
map: 2003000
comprehension: 1592000
ok
4>
(Win2K with R9B)
Regards,
Dominic.
More information about the erlang-questions
mailing list