[erlang-questions] How do I elegantly check many conditions?
Hynek Vychodil
vychodil.hynek@REDACTED
Fri Mar 20 18:33:37 CET 2009
One benchmark is worth of thousand questions ;-)
-module(exceptiontest).
-export([test/2, success/0, fail/0]).
test(0, _)-> ok;
test(N, F)->
try
ok = ?MODULE:F()
catch
error:{badmatch, fail} -> fail
end,
test(N-1, F).
success()->ok.
fail()->fail.
Byte code:
9> timer:tc(exceptiontest,test,[1000000, fail]).
{469954,ok}
10> timer:tc(exceptiontest,test,[1000000, success]).
{164390,ok}
Native:
11> c(exceptiontest, native).
{ok,exceptiontest}
12> timer:tc(exceptiontest,test,[1000000, fail]).
{146526,ok}
13> timer:tc(exceptiontest,test,[1000000, success]).
{21573,ok}
It means about roughly 2 millions exceptions in one second (0.47us) compared
to 6 millions success (external) function calls (0.146us) - one can guess
that exception handling takes about 0.32us.
In native code it is 6.8 vs 47 millions per second and handling can take
about 0.125us. There can be some additional cost for try-catch construct
which I haven't measured this way. (Is very low how I remember from past.)
On Fri, Mar 20, 2009 at 3:43 PM, ryeguy <ryeguy1@REDACTED> wrote:
> I actually like that one the best. It's cleanest, and simplest. I
> actually thought about something like this, but my mind is still
> clouded from other evil languages, where exceptions should be used for
> "exceptional" situations (exluding Python).
>
> Is exception handling cheap in Erlang?
>
> On Mar 20, 10:34 am, Paul Fisher <pfis...@REDACTED> wrote:
> > Hynek Vychodil wrote:
> > > One of Joe's suggestion: program success case code separated from error
> > > handling. You can make it in this way
> >
> > > create_user(Email, UserName, Password) ->
> > > try
> > > ok = new_email(Email),
> > > ok = valid_user_name(UserName),
> > > ok = new_user(UserName),
> > > ok = strong_password(Password),
> > > ...
> > > _create_user(Email, UserName, Password)
> > > catch
> > > error:{badmatch, email_in_use} -> do_something();
> > > error:{badmatch, invalid_user_name} -> do_something();
> > > error:{badmatch, user_exists} -> do_something();
> > > error:{badmatch, weak_password} -> do_something();
> > > ...
> > > end.
> >
> > +1 Bravo!
> >
> > --
> > paul
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questi...@REDACTED://
> www.erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
--
--Hynek (Pichi) Vychodil
Analyze your data in minutes. Share your insights instantly. Thrill your
boss. Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090320/ee733a30/attachment.htm>
More information about the erlang-questions
mailing list