[erlang-questions] How expensive are guard clauses?

Richard Carlsson <>
Thu Jan 28 12:22:12 CET 2016


See http://erlang.org/doc/man/assert.hrl.html (requires Erlang/OTP 18 or
later, otherwise the same macros can only be accessed by including
eunit.hrl).



        /Richard

2016-01-28 12:01 GMT+01:00 Roger Lipscombe <>:

> I'm considering adding more-expressive guard clauses to some of my
> functions, in order to prevent garbage data getting past certain
> points in my application.
>
> If I have a macro like something the following...
>
>     -define(is_valid_id(Id),
>         (is_binary(Id) andalso byte_size(Id) =:= 16 andalso Id =/=
> <<"0000000000000000">>)).
>
> ...and I use it in the following way...
>
>     frobnicate(StudentId, CourseId) when ?is_valid_id(StudentId),
> ?is_valid_id(CourseId) ->
>         ...
>
> ...just how expensive, in terms of timing, is that? Is it something I
> need to worry about? Can I scatter these guards everywhere? Should I
> restrict them to a few places? Should I make it so that they can be
> turned off in production (through a compiler define)?
>
> Obviously, I'll be looking to actually measure it, but I'm wondering
> if anyone's tried this and got anything to suggest.
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160128/b7a24d31/attachment.html>


More information about the erlang-questions mailing list