[erlang-questions] bit syntax slower than bit operations

Matthias Radestock <>
Sat Sep 1 22:23:44 CEST 2007


Bob Ippolito wrote:
> On 9/1/07, Matthias Radestock <> wrote:
>> encode_compact({B2, B3, B4, B5}) ->
>>      <<0:2, B5:1, B4:1, B3:1, B2:1, 0:2>>.
>> encode_verbose({B2, B3, B4, B5}) ->
>>      <<((B2 bsl 2) bor (B3 bsl 3) bor (B4 bsl 4) bor (B5 bsl 5)):8>>.
>> [...]
>> Unfortunately, my tests indicate that the latter version is about twice
>> as fast, using R11B-5 under Linux on x86.
> 
> I don't know enough about the compiler or interpreter to answer your
> question, but note that encode_verbose is not the same as
> encode_compact. Consider any of B2 through B5 being integers outside
> of 0 or 1. For a complete translation you'd need some extra (BN band
> 1) ops.

Well spotted. Adding that mask slows down encode_verbose somewhat, but 
it is still substantially faster than encode_compact.

Matthias.



More information about the erlang-questions mailing list