[erlang-questions] binary_to_integer/1 and binary_to_float/1 and friends

Bob Ippolito <>
Mon Apr 30 20:13:24 CEST 2012

On Mon, Apr 30, 2012 at 10:54 AM, Max Lapshin <> wrote:

> > defined in a macro) and there is an unnecessary conversion cost to list
> > (although the cost is small since both binary and list are likely small).
> >
> Cost is not so small, because NIF binary_to_integer will create only
> one object, when list_to_integer(binary_to_list()) will create many of
> them. It is very important when you need to import LOTS of numbers
> (read about my CSV parsing).

I suppose you could implement it in pure erlang without creating any
intermediate lists, maybe it's even fast with HIPE. Floats are likely
another story.

binary_to_integer(B=(<<_N, _/binary>>)) ->
    binary_to_integer(B, 0).

binary_to_integer(<<N, Rest/binary>>, Acc) when N >= $0 andalso N =< $9 ->
    binary_to_integer(Rest, (N - $0) + (Acc * 10));
binary_to_integer(<<>>, Acc) ->

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120430/109a4e2c/attachment.html>

More information about the erlang-questions mailing list