[erlang-questions] Fast binary reverse

Antoine Koener <>
Thu Oct 27 12:26:04 CEST 2011


Keep in mind that lists:reverse does not reverse a list, but instead change
the walking direction and the starting point...


On Thu, Oct 27, 2011 at 12:11 PM, Ward Bekker <> wrote:

> **
> Hi Michael,
>
> Yes, Funs will give overhead. Badly chosen example code on my part.
>
> But still the rev2 example is on my machine still twice or trice as slow
> (YMMV) than a list:reverse.
>
> I want to see what can be done to bring a binary reverse on par with a list
> reverse. I would, naïvely, expect a binary reverse to be faster than a
> erlang list reverse.
>
> Regards,
>
> Ward
>
>
> ------------------------------
> *From:* Michael Uvarov [mailto:]
> *To:* Ward Bekker [mailto:]
> *Sent:* Thu, 27 Oct 2011 10:33:12 +0200
> *Subject:* Re: [erlang-questions] Fast binary reverse
>
>
> Just don't use anonymous functions.
>
> <code>
> -module(bin).
> -export([rev1/1, rev2/1]).
>
>
> rev1(B) ->
>
> S = size(B)*8,
> <<X:S/integer-little>>=B,
> <<X:S/integer-big>>.
>
> rev2(B) ->
> binary:list_to_bin(
> lists:reverse(
> binary:bin_to_list(B))).
> </code>
>
>
> 29> timer:tc(bin,rev2,[<<"teribly">>]).
> {5,<<"ylbiret">>}
> 30> timer:tc(bin,rev1,[<<"teribly">>]).
> {11,<<"ylbiret">>}
>
> --
> Best regards,
> Uvarov Michael
>
>
> _______________________________________________
> 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/20111027/cdbca4c4/attachment.html>


More information about the erlang-questions mailing list