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

Nico Kruber <>
Tue Mar 29 17:57:06 CEST 2011


  On Tuesday 29 March 2011 17:34:29 you wrote:
> Paulo Sérgio Almeida wrote:
> > On 3/29/11 3:10 PM, Nico Kruber wrote:

> > 
> > 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).
> {ok,t}
> 2> timer:tc(t, t2b, [1000000]).
> {133505,ok}
> 3> timer:tc(t, c2b, [1000000]).
> {636624,ok}
> 
> 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.
> 
> Kostis
> 
> %%==============================================================
> -module(t).
> 
> -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,
> lists:seq(1,N)).

the lists:seq(1,1000000) will additionally slow down the process as it will 
create the whole list at first
-> I used the following loop for my benchmark:
%%==============================================================
-spec iter(Count::pos_integer(), F::fun(() -> any()), Tag::string()) -> ok.
iter(Count, F, Tag) ->
    F(),
    Start = erlang:now(),
    iter_inner(Count, F),
    Stop = erlang:now(),
    ElapsedTime = timer:now_diff(Stop, Start) / 1000000.0,
    Frequency = Count / ElapsedTime,
    ct:pal("~p iterations of ~p took ~ps: ~p1/s~n",
           [Count, Tag, ElapsedTime, Frequency]),
    ok.

-spec iter_inner(Count::pos_integer(), F::fun(() -> any())) -> ok.
iter_inner(0, _) ->
    ok;
iter_inner(N, F) ->
    F(),
    iter_inner(N - 1, F).
%%==============================================================

regarding 2 vs 20: I simply misread the numbers :(

Nico
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20110329/b96195d4/attachment.bin>


More information about the erlang-bugs mailing list