io:format() vs io_lib:format()

Raimo Niskanen <>
Fri Feb 28 10:51:11 CET 2003

A trick question for anyone interested:

What is the difference between:
	io:format(Format, Args)
	io:put_chars(io_lib:format(Format, Args))

A hint, it's about performance.

Scroll down to see the answer.

/ Raimo Niskanen, Erlang/OTP, Ericsson AB.


io:format(Format, Args) will send Format and Args to the io server 
process, do the formatting there and send the data to a port (mostly).

io:put_chars(io_lib:format(Format, Args)) will do the formatting in the 
calling process and send the formatted result to the io server process 
that sends the data to a port.

So, since the formatted data mostly is bigger than the sum of Format and 
Args, the second version sends more data between processes. Hence the 
first version is mostly more efficient.

Raimo Niskanen wrote:
> io:format(Format, Args) uses io_lib:format(Format, Args) to create the 
> data that is written to stdout, so there should be absolutely no 
> difference in the result. Apart from that io_lib:format/2 returns a deep 
> string, and io:format/2 just does a side effect.
> io:put_chars(io_lib:format(Format, Args)) would be exactly equivalent to 
> io:format(Format, Args).
> The feature in io_lib:format/2's return value is that the string need 
> not be flattened if it is supposed to be sent to e.g io:put_chars/1, 
> file:write/2 or erlang:port_command/2. It is a perfectly valid IO-list 
> and it is wasting time to flatten it if it is not needed.
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> Bengt Kleberg wrote:
>> greetings,
>> are the differences between io:format() and io_lib:format() documented
>> anywhere? or is there no difference?
>> 3> io:format( "asd~s~n", ["asd"]).
>> asdasd
>> ok
>> 4> io_lib:format( "asd~s~n", ["asd"]).
>> [97,115,100,"asd","\n"]
>> io:format() gives me a single string.
>> io_lib:format() gives me a string with a string as element 3 (0 based).
>> is this a bug or a feature? or is it a result of flattening the string
>> as it goes to stdout?
>> bengt

More information about the erlang-questions mailing list