[erlang-questions] byte_size/1 vs variable access
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
1> Foo = <<"asdf">>, FooSize = byte_size(Foo).
2> <<Foo:(byte_size(Foo))/binary, Rest/binary>> = <<"asdfghi">>.
* 1: illegal bit size
3> <<Foo:FooSize/binary, Rest/binary>> = <<"asdfghi">>.
On Tue, Feb 25, 2014 at 1:56 PM, Loïc Hoguin <> wrote:
> 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
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions