[erlang-questions] Fast binary reverse

Ward Bekker <>
Thu Oct 27 10:12:54 CEST 2011


Hi,


I want to reverse the bytes of a bitstring as fast as possible. There is not a erlang BIF that does this, so I googled some possible solutions:



Fun1 = fun(B) -> S = size(B)*8,<<X:S/integer-little>>=B, <<X:S/integer-big>> end.
Fun2 = fun(B) -> binary:list_to_bin(lists:reverse(binary:bin_to_list(B))) end.


Fun1 = ~37 microseconds when executing Fun1(<"terribly">).
Fun2 = ~16 microseconds when executing Fun2(<"terribly">)


But a simple reverse of a list of integers is much, much faster:


timer:tc(lists, reverse, ["terribly"]) = ~1 microseconds.


Are there other options or do I need to write a new BIF function? 


Regards,


Ward Bekker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111027/6099deb9/attachment.html>


More information about the erlang-questions mailing list