[erlang-questions] byte_size/1 vs variable access

Bob Ippolito <>
Tue Feb 25 23:01:32 CET 2014


I don't know about performance implications, but I've found that it's often
necessary to work around limitations of bit matching syntax. Here's an
example:

1> Foo = <<"asdf">>, FooSize = byte_size(Foo).
4
2> <<Foo:(byte_size(Foo))/binary, Rest/binary>> = <<"asdfghi">>.
* 1: illegal bit size
3> <<Foo:FooSize/binary, Rest/binary>> = <<"asdfghi">>.
<<"asdfghi">>


On Tue, Feb 25, 2014 at 1:56 PM, Loïc Hoguin <> wrote:

> Hello,
>
> Been wondering something pretty much insignificant recently.
>
> When I need the size of a binary I tend to create a variable, say:
>
> DataSize = byte_size(Data)
>
> and then I reuse this variable in the function.
>
> But considering byte_size/1 is O(1) I am wondering if perhaps that's a
> little pointless. Is it even worth creating a variable for this? Is perhaps
> the variable optimized out? Perhaps accessing a variable contents and
> calling byte_size/1 are equivalent operations? Or the GC that will follow
> is not worth what little is saved by creating a variable in the first place?
>
> If someone could shed some light on this perhaps I could stop creating
> variables like this and simplify my code a little more.
>
> Thank you.
>
> --
> Loïc Hoguin
> http://ninenines.eu
> _______________________________________________
> 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/20140225/777f40e3/attachment.html>


More information about the erlang-questions mailing list