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

50 views
Skip to first unread message

Bernardo Alvez

unread,
Oct 1, 2009, 6:52:10 AM10/1/09
to erlang-q...@erlang.org
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

Michael Richter

unread,
Oct 1, 2009, 8:55:29 AM10/1/09
to Bernardo Alvez, Erlang Users' List
You could try an experiment, maybe?

File: "output":

#! /usr/bin/env escript
%% -mode(compile).

main(["list"]) ->
M1 = erlang:process_info(self(), memory),
T1 = erlang:now(),
list_loop(500),
T2 = erlang:now(),
M2 = erlang:process_info(self(), memory),
io:format("~n~n~w , ~w , ~w~n~n", [M1, M2, timer:now_diff(T2,T1)]);

main(["binary"]) ->
M1 = erlang:process_info(self(), memory),
T1 = erlang:now(),
binary_loop(500),
T2 = erlang:now(),
M2 = erlang:process_info(self(), memory),
io:format("~w , ~w , ~w~n", [M1, M2, timer:now_diff(T2,T1)]);

main(_) ->
io:format("Try again.~n").

list_loop(0) ->
io:format("Done!~n~n");

list_loop(N) ->
io:format("hello ~s", ["world"]),
list_loop(N-1).

binary_loop(0) ->
io:format(<<"Done!~n~n">>);

binary_loop(N) ->
io:format(<<"hello ~s">>, [<<"world">>]),
binary_loop(N-1).

Result set from several runs of "clear ; ./output list":

- {memory,54492} , {memory,33820} , 104308
- {memory,54492} , {memory,33820} , 103686
- {memory,54492} , {memory,33820} , 100892
- {memory,54492} , {memory,33820} , 87483
- {memory,54492} , {memory,33820} , 99644

Result set from several runs of "clear ; ./output binary":

- {memory,44156} , {memory,33820} , 94222
- {memory,44156} , {memory,33820} , 110587
- {memory,44156} , {memory,33820} , 115329
- {memory,44156} , {memory,33820} , 111326
- {memory,44156} , {memory,33820} , 102580

I make no representation that my code is any good (indeed it's the
opposite), but a brief eyeball of the results shows that binaries use up a
little less memory and a little more CPU. I'm certain the other, better
Erlang users can give you better benchmarking code to answer your question
(but so can you, and your benchmarking code would reflect your intended
use).

2009/10/1 Bernardo Alvez <man...@adinet.com.uy>

Bernardo Alvez

unread,
Oct 1, 2009, 6:12:49 PM10/1/09
to Michael Richter, Erlang Users' List
Thanks Michael!

BR,
Bernardo

Michael Richter escribió:

Roberto Aloi

unread,
Oct 2, 2009, 8:48:29 AM10/2/09
to Bernardo Alvez, erlang-q...@erlang.org
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

Reply all
Reply to author
Forward
0 new messages