Traversing a binary
Thomas Lindgren
thomasl_erlang@REDACTED
Tue Sep 21 18:45:05 CEST 2004
--- Javier París Fernández <paris@REDACTED> wrote:
> 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?
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?)
An Erlang compiler could theoretically generate code
close to the C code, by recognizing (a) that we are
iterating over a binary and hoisting tests and
indirections out of the loop and (b) by recognizing
that the '+' can be strength-reduced to something less
expensive (perhaps all the way down to C:s weird
arithmetic, which lacks even overflow detection :-).
As far as I know, no Erlang compiler can do that.
There is also another possibility. A few weeks ago, in
the context of binary comprehensions, I floated the
concept of fold operations over binaries. If one would
restrict the operations suitably, a fast checksum
might look like
binary:foldl('16-bit +', 0, Bin)
or something like that. ("Fast" in the sense that the
runtime system could implement this family of
operations by predefined loops, and in the sense that
a compiler could more easily understand what was
desired.)
But that is in the future, if at all. At this point in
time, I would tentatively recommend using your C
driver.
Best,
Thomas
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail
More information about the erlang-questions
mailing list