Traversing a binary

Erik Stenman erik.stenman@REDACTED
Tue Sep 21 22:52:09 CEST 2004


Javier París Fernández wrote:

>On Tue, Sep 21, 2004 at 09:45:05AM -0700, Thomas Lindgren wrote:
>  
>
>>The C code will be difficult to beat in this sort of
>>program. I can't see any obvious way to improve on the
>>program above either. (Though I wonder if you really
>>want '+' or 'bxor' in the above?)
>>    
>>
>
>It's the Tcp checksum, and it is defined that way in the 
>rfc. I agree that it is a bit strange.
>  
>
I'm no Tcp expert, but are you sure?
Is it not defined as the 16-bit 1's complement sum?
I.e. something like:
'+'(A,B) ->
    S = A + B,
    S band 16#ffff + (S bsr 16).

One problem with your code is that it generates bignums (heap-allocated 
large integers),
but if it is the 16-bit 1's complement sum, then you don't need large 
integers.

You could also traverse the binary without creating new sub-binaries, by 
keeping
track of how many byte-pairs you have seen (N):

c(N,Bin,Csum) ->
    case Bin of
    <<_:N/binary, Num:16/integer,_/binary>> ->
        c(N+2,Bin,'+'(Csum,Num));
        <<_:N/binary, Num:8/integer>> ->
        '+'(Csum,Num bsl 8);
    _ -> Csum
    end.

Unfortunately even with these two changes the code does not become much 
faster...
I have attached a test program that calculates the checksum in 4 
different ways,
and prints the execution times in microseconds, but the function c/3 
above is the fastest.

/Erik
 - I'm Happi, you should be happy.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20040921/7df7fef6/attachment.htm>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: tc.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20040921/7df7fef6/attachment.ksh>


More information about the erlang-questions mailing list