[erlang-questions] Fast binary reverse

Ward Bekker ward@REDACTED
Thu Oct 27 12:11:29 CEST 2011


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:freeakk@REDACTED]
To: Ward Bekker [mailto:ward@REDACTED]
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
    
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111027/eb6eddac/attachment.htm>


More information about the erlang-questions mailing list