Traversing a binary

Javier París Fernández paris@REDACTED
Tue Sep 21 17:46:26 CEST 2004


On Tue, Sep 21, 2004 at 05:16:05PM +0200, Raimo Niskanen wrote:
> Post your code, there are fast and slow ways to traverse a binary.

Ok, I only tried two things:

checksum_1(<<Num:16/integer, Remainder/binary>>, Csum) ->
    checksum_1(Remainder, Csum + Num);
checksum_1(<<>>, Csum) ->
    Csum.

The problem here seems to be the generation of sub-binaries. I tried to
get more bytes at a time to reduce the number of binaries generated:

checksum_1(Bin = <<N1:16/integer, N2:16/integer, N3:16/integer, 
	N4:16/integer, N5:16/integer, N6:16/integer, 
	N7:16/integer, N8:16/integer, Rem/binary>>, 
	Csum) when size(Bin)>=16 ->
  checksum_1(Rem, Csum+N1+N2+N3+N4+N5+N6+N7+N8);

Ok, this one was faster, but it is still much faster to do it in a
C driver, specially if the binary is big (over 1 Mb). Is there a faster
way to do this?

Regards.




More information about the erlang-questions mailing list