[erlang-questions] Proper replacement for io_lib:format due to low speed
Loïc Hoguin
essen@REDACTED
Sun Mar 22 11:51:13 CET 2015
If a binary isn't needed then the list_to_binary can be removed
entirely, and for bonus points integer_to_binary can be used instead of
integer_to_list to save some memory.
If a binary is needed it should be quicker to build the binary directly
instead of having an intermediate list. If benchmarks show the same time
to produce both you still win because you then don't have to GC
intermediate representations (assuming no optimization takes place).
Cheers,
On 03/22/2015 11:10 AM, Chandru wrote:
> Hi Max,
>
> Is something like this not good enough in terms of speed?
>
> iso8601({{Y,Mo,D}, {H,Mn,S}}) ->
> list_to_binary([integer_to_list(Y), $-,
> two_digit_str(Mo), $-,
> two_digit_str(D), $T,
> two_digit_str(H), $:,
> two_digit_str(Mn), $:,
> two_digit_str(S), $Z]).
>
> two_digit_str(X) when X < 10 ->
> [$0 | integer_to_list(X)];
> two_digit_str(X) ->
> integer_to_list(X).
>
>
> cheers,
> Chandru
>
>
> On 22 March 2015 at 09:01, Max Lapshin <max.lapshin@REDACTED
> <mailto:max.lapshin@REDACTED>> wrote:
>
> Hi.
>
> I've met again performance problem with io_lib:format.
>
> iso8601({{Y,Mo,D}, {H,Mn,S}}) ->
> FmtStr = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
> IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
> list_to_binary(IsoStr).
>
>
> Such code is a 0,01% of business logic of one small part of
> flussonic, but it takes about 95% of time to make 10% of text output.
>
>
> I've solved this problem via https://github.com/maxlapshin/io_libc
> but I'm not sure that it is
> a best way to do. Maybe this is better solved in pure erlang?
>
>
> Perhaps I'm not alone with this problem and somebody has some better
> idea than just exposing fprinf via nif?
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
--
Loïc Hoguin
http://ninenines.eu
More information about the erlang-questions
mailing list