[erlang-questions] Better use of io:format and io_lib:format

Roberto Aloi <>
Fri Oct 2 14:48:29 CEST 2009


Hi Bernardo,

you might want to try something like this:

-module(benchmark).

-export([test/1, with_list/1, with_binary/1]).

with_list(0) ->
    ok;
with_list(N) ->
    io:format("hello ~s", ["world"]),
    with_list(N-1).

with_binary(0) ->
    ok;
with_binary(N) ->
    io:format(<<"hello ~s">>, [<<"world">>]),
    with_binary(N-1).

test(Num) ->
    {TimeList, _} = timer:tc(?MODULE, with_list, [Num]),
    {TimeBinary, _} = timer:tc(?MODULE, with_binary, [Num]),
    {{with_list, TimeList}, {with_binary, TimeBinary}}.

You will be able to see how performances degrade for strings by trying
the test/1 function with different values. For example:

>benchmark:test(1).
{{with_list,32},{with_binary,19}}

> benchmark:test(10).
{{with_list,6253},{with_binary,262}}

> benchmark:test(100).
{{with_list,11329},{with_binary,5572}}

It would be nice to try this with R12 and R13, for a comparison.
You might also be interested in the function:
memory().

Regards,

Roberto Aloi
Erlang Training and Consulting Ltd.
http://erlang-consulting.com
http://aloiroberto.wordpress.com


Bernardo Alvez wrote:
> Hi all,
>
> Which  of the following is better to use in terms of performance and
> resources usage?:
>
> io:format("hello ~s", ["world"]).
> or
> io:format(<<"hello ~s">>, [<<"world">>]).
>
> Thanks,
> Bernardo
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   



More information about the erlang-questions mailing list