<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Testament to what a friendly language erlang is: the learner effortlessly arrives at the most fitting solution.<br><br>--<br>festina lente<div><br></div></div><div><br>On 10 Jan 2015, at 12:06, Sergej Jurecko <<a href="mailto:sergej.jurecko@gmail.com">sergej.jurecko@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">Async version so results are less skewed by GC. <div>With a larger bin it is more clear what the best solution is:</div><div><div><div><br></div><div>bin_reverse:reverse_test(crypto:rand_bytes(100000)).</div><div>"list"=15515</div><div>"tail_pos"=24061</div><div>"list_to_bin"=28627</div><div>"body_pos"=4781891</div></div><div><br></div><div><div>-module(bin_reverse).</div><div>-export([reverse_test/1]).</div><div><br></div><div><br></div><div>reverse_test(Bin) -></div><div> [</div><div> async("body_pos",fun reverse_body_pos/1,Bin),</div><div> async("tail_pos",fun reverse_tail_pos/1,Bin),</div><div> async("list",fun reverse_list/1,Bin),</div><div> async("list_to_bin",fun reverse_list_to_bin/1,Bin)</div><div> ].</div><div><br></div><div>async(Name,Fun,Bin) -></div><div> spawn(fun() -></div><div> {T,_} = timer:tc(Fun,[Bin]),</div><div> io:format("~p=~p~n",[Name,T])</div><div> end).</div><div><br></div><div><br></div><div>reverse_tail_pos(Bin) -></div><div> reverse1(Bin, byte_size(Bin), <<>>).</div><div><br></div><div>reverse1(_, 0, Acc) -> Acc;</div><div>reverse1(Bin, Pos, Acc) -></div><div> reverse1(Bin, Pos - 1, <<Acc/binary, (binary:at(Bin, Pos - 1))>>).</div><div><br></div><div><br></div><div><br></div><div>reverse_body_pos(Bin) -></div><div> reverse2(Bin, byte_size(Bin)).</div><div><br></div><div>reverse2(_, 0) -> <<>>;</div><div>reverse2(Bin, Pos) -></div><div> <<(binary:at(Bin, Pos - 1)), (reverse2(Bin, Pos - 1))/binary >>.</div><div><br></div><div><br></div><div><br></div><div>reverse_list(Bin) -></div><div> list_to_binary(lists:reverse(binary_to_list(Bin))).</div><div><br></div><div><br></div><div><br></div><div>reverse_list_to_bin(Bin) -></div><div> reverse3(Bin, []).</div><div><br></div><div>reverse3(<<>>, Acc) -> list_to_binary(Acc);</div><div>reverse3(<<C,Bin/binary>>, Acc) -> reverse3(Bin, [C|Acc]).</div><div><br></div><div><br><div><div>On 10 Jan 2015, at 12:46, Сергей Прохоров <<a href="mailto:seriy.pr@gmail.com">seriy.pr@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Ok, looks like topicstarter's solution is the fastest one:<div><br></div><div>=======================</div><div><div>-module(bin_reverse).</div><div>-export([reverse_test/1]).</div><div><br></div><div><br></div><div>reverse_test(Bin) -></div><div> [</div><div> {"body_pos", timer:tc(fun reverse_body_pos/1, [Bin])},</div><div> {"tail_pos", timer:tc(fun reverse_tail_pos/1, [Bin])},</div><div> {"list", timer:tc(fun reverse_list/1, [Bin])},</div><div> {"list_to_bin", timer:tc(fun reverse_list_to_bin/1, [Bin])}</div><div> ].</div><div><br></div><div><br></div><div><br></div><div>reverse_tail_pos(Bin) -></div><div> reverse1(Bin, byte_size(Bin), <<>>).</div><div><br></div><div>reverse1(_, 0, Acc) -> Acc;</div><div>reverse1(Bin, Pos, Acc) -></div><div> reverse1(Bin, Pos - 1, <<Acc/binary, (binary:at(Bin, Pos - 1))>>).</div><div><br></div><div><br></div><div><br></div><div>reverse_body_pos(Bin) -></div><div> reverse2(Bin, byte_size(Bin)).</div><div><br></div><div>reverse2(_, 0) -> <<>>;</div><div>reverse2(Bin, Pos) -></div><div> <<(binary:at(Bin, Pos - 1)), (reverse2(Bin, Pos - 1))/binary >>.</div><div><br></div><div><br></div><div><br></div><div>reverse_list(Bin) -></div><div> list_to_binary(lists:reverse(binary_to_list(Bin))).</div><div><br></div><div><br></div><div><br></div><div>reverse_list_to_bin(Bin) -></div><div> reverse3(Bin, []).</div><div><br></div><div>reverse3(<<>>, Acc) -> list_to_binary(Acc);</div><div>reverse3(<<C,Bin/binary>>, Acc) -> reverse3(Bin, [C|Acc]).</div></div><div><br></div><div>=======================</div><div><br></div><div><div>24> bin_reverse:reverse_test(crypto:rand_bytes(100)).</div><div>[{"body_pos",</div><div> {32,</div><div> <<247,128,13,6,1,53,110,148,74,210,196,126,234,115,180,</div><div> 85,33,206,130,164,3,242,174,157,...>>}},</div><div> {"tail_pos",</div><div> {14,</div><div> <<247,128,13,6,1,53,110,148,74,210,196,126,234,115,180,</div><div> 85,33,206,130,164,3,242,174,...>>}},</div><div> {"list",</div><div> {3,</div><div> <<247,128,13,6,1,53,110,148,74,210,196,126,234,115,180,</div><div> 85,33,206,130,164,3,242,...>>}},</div><div> {"list_to_bin",</div><div> {13,</div><div> <<247,128,13,6,1,53,110,148,74,210,196,126,234,115,180,</div><div> 85,33,206,130,164,3,...>>}}]</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-01-10 14:30 GMT+03:00 Ivan Uemlianin <span dir="ltr"><<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Remember lists:reverse/1 is implemented in C & does some non-erlangy things (IIRC) so should be jolly fast & efficient. Unless binary_to_list/1 is v bad the OP's version is the best so far (imho).<br>
<br>
Best wishes<br>
<br>
Ivan<br>
<br>
<br>
--<br>
festina lente<br>
<div><div class="h5"><br>
<br>
> On 10 Jan 2015, at 11:24, Сергей Прохоров <<a href="mailto:seriy.pr@gmail.com">seriy.pr@gmail.com</a>> wrote:<br>
><br>
> Probably this one will be more effective<br>
><br>
> reverse(Bin) -><br>
> reverse(Bin, byte_size(Bin)).<br>
><br>
> reverse(_, 0) -> <<>>;<br>
> reverse(Bin, Pos) -><br>
> <<(binary:at(Bin, Pos - 1)), (reverse(Bin, Pos - 1))/binary >>.<br>
</div></div>> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br></div></div></div></div></blockquote></body></html>