Hm. The Erlang people talk of "Pattern matching in function head and in case and receive clauses are optimized by the compiler." & "the compiler is free to rearrange the clauses. It will generate code similar to this".Of course it's optimized. Like you would expect from a language where the only way to loop is recursion.
Yet, this works in Erlang:
Just for comparison (with Perls):f(1) -> io:format("I got one!"); f(X) when X rem 2 == 0 -> io:format("I got even!"); f(X) when X rem 2 == 1 -> io:format("I got odd! (but not one)"). ... f(random:uniform(1000)). f("Now eat this!"). %% runtime error
output:-module(fns). -export([run_test/0]). f(X, Acc) when X rem 2 == 0 -> Acc; f(X, Acc) when X rem 2 == 1 -> Acc + 1. run_test() -> TSeq = [random:uniform(10) || _ <- lists:seq(1, 1000000)], {Time, Result} = timer:tc(fun() -> lists:foldl(fun f/2, 0, TSeq) end), io:format("~w microseconds~nResult: ~w~n", [Time, Result]).
So, functions that actually do something in Erlang (count odd numbers) are three times faster then Perls functions that do nothing whatsoever.93267 microseconds Result: 500464
In reply to Re^9: rough approximation to pattern matching using local (Multi Subs)
by Anonymous Monk
in thread rough approximation to pattern matching using local
by gregory-nisbet
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |