[erlang-bugs] Re: UTF8 string handling in different erlang:*** functions

Kostis Sagonas <>
Tue Mar 29 17:34:29 CEST 2011

Paulo Sérgio Almeida wrote:
> On 3/29/11 3:10 PM, Nico Kruber wrote:
>>> 1000000 iterations of "erlang:term_to_binary/1" took 0.02946s:
>> 33944331.2966734541/s
>>> 1000000 iterations of "unicode:characters_to_binary/1" took 0.667519s:
>> 1498084.69871269591/s
>> ->  looks like I should chose erlang:term_to_binary/1 since at least 
>> on my
>> machine is is around twice as fast.
> It's not twice but 20 times as fast. Amazing. Even though it should be 
> slower, this slower is surprising.

I have trouble reproducing these numbers, both the 2 and the 20.
With the program at the end of this mail, on an x86_64, I get:

Eshell V5.8.3  (abort with ^G)
1> c(t).
2> timer:tc(t, t2b, [1000000]).
3> timer:tc(t, c2b, [1000000]).

which makes the term_to_binary version about 4 times as fast on this 
machine.  On a 32-bit machine the difference is about 6 - 6.5 times.



-export([t2b/1, c2b/1]).

-define(S, "some medium sized string here").

t2b(N) ->
   lists:foreach(fun (_) -> erlang:term_to_binary(?S) end, lists:seq(1,N)).

c2b(N) ->
   lists:foreach(fun (_) -> unicode:characters_to_binary(?S) end, 

More information about the erlang-bugs mailing list