[erlang-questions] shell is much slower than escript ?

月忧茗 yueyoum@REDACTED
Tue Sep 27 20:39:41 CEST 2016


I have s simple code to create and update maps.
and using timer:tc to measure the time costed by this operations.


file aa.erl


-module(aa).

-export([create/1,
        update/1]).

-record(person, {id, x, y, angle, speed}).

create(Num) ->
    create(Num, #{}).

update(M) ->
    Size = maps:size(M),
    Fun = fun(K, V) ->
                  X = Size - K,
                  V#person{x=X,y=X,angle=X,speed=X}
          end,

    maps:map(Fun, M).


create(0, M) ->
    M;

create(N, M) ->
    M1 = M#{N => #person{id=N, x=N, y=N, angle=N, speed=N}},
    create(N-1, M1).


-----------------------------
file aa.escript

#!/usr/bin/env escript


main(_) ->
    io:format("Start~n"),


    {T, M} = timer:tc(aa, create, [10000]),
    io:format("create cost ms: ~p~n", [T / 1000]),

    {T1, M1} = timer:tc(aa, update, [M]),
    io:format("update cost ms: ~p~n", [T1 / 1000]),
    ok.

---------------------------------------

When run with escript, the outputs are:


wang[02:31][~/Learn]$ ./aa.escript
./aa.escript:11: Warning: variable 'M1' is unused
Start
create cost ms: 7.344
update cost ms: 5.903
wang[02:31][~/Learn]$ ./aa.escript
./aa.escript:11: Warning: variable 'M1' is unused
Start
create cost ms: 7.077
update cost ms: 5.484
wang[02:31][~/Learn]$ ./aa.escript
./aa.escript:11: Warning: variable 'M1' is unused
Start
create cost ms: 7.006
update cost ms: 5.438
wang[02:31][~/Learn]$


--------------------------------------------

But when I run in erl shell,  the outputs are:

wang[02:32][~/Learn]$ erl
Erlang/OTP 19 [erts-8.0] [source] [64-bit] [smp:4:4] [async-threads:10]
[hipe] [kernel-poll:false]

Eshell V8.0  (abort with ^G)
1> {T, M} = timer:tc(aa, create, [10000]).
{19523,
 #{462 => {person,462,462,462,462,462},
   704 => {person,704,704,704,704,704},
   7994 => {person,7994,7994,7994,7994,7994},
   6850 => {person,6850,6850,6850,6850,6850},
   6836 => {person,6836,6836,6836,6836,6836},
   9765 => {person,9765,9765,9765,9765,9765},
   6653 => {person,6653,6653,6653,6653,6653},
   5961 => {person,5961,5961,5961,5961,5961},
   1328 => {person,1328,1328,1328,1328,1328},
   4351 => {person,4351,4351,4351,4351,4351},
   8373 => {person,8373,8373,8373,8373,8373},
   6749 => {person,6749,6749,6749,6749,6749},
   9406 => {person,9406,9406,9406,9406,9406},
   8441 => {person,8441,8441,8441,8441,8441},
   6570 => {person,6570,6570,6570,6570,6570},
   6074 => {person,6074,6074,6074,6074,6074},
   3590 => {person,3590,3590,3590,3590,3590},
   7636 => {person,7636,7636,7636,7636,7636},
   5934 => {person,5934,5934,5934,5934,5934},
   4631 => {person,4631,4631,4631,4631,4631},
   1935 => {person,1935,1935,1935,1935,1935},
   8406 => {person,8406,8406,8406,8406,...},
   9860 => {person,9860,9860,9860,...},
   3742 => {person,3742,3742,...},
   9221 => {person,9221,...},
   4969 => {person,...},
   9846 => {...},...}}
2> T / 1000.
19.523
3>
3> {T1, M1} = timer:tc(aa, update, [M]).
{16920,
 #{462 => {person,462,9538,9538,9538,9538},
   704 => {person,704,9296,9296,9296,9296},
   7994 => {person,7994,2006,2006,2006,2006},
   6850 => {person,6850,3150,3150,3150,3150},
   6836 => {person,6836,3164,3164,3164,3164},
   9765 => {person,9765,235,235,235,235},
   6653 => {person,6653,3347,3347,3347,3347},
   5961 => {person,5961,4039,4039,4039,4039},
   1328 => {person,1328,8672,8672,8672,8672},
   4351 => {person,4351,5649,5649,5649,5649},
   8373 => {person,8373,1627,1627,1627,1627},
   6749 => {person,6749,3251,3251,3251,3251},
   9406 => {person,9406,594,594,594,594},
   8441 => {person,8441,1559,1559,1559,1559},
   6570 => {person,6570,3430,3430,3430,3430},
   6074 => {person,6074,3926,3926,3926,3926},
   3590 => {person,3590,6410,6410,6410,6410},
   7636 => {person,7636,2364,2364,2364,2364},
   5934 => {person,5934,4066,4066,4066,4066},
   4631 => {person,4631,5369,5369,5369,5369},
   1935 => {person,1935,8065,8065,8065,8065},
   8406 => {person,8406,1594,1594,1594,...},
   9860 => {person,9860,140,140,...},
   3742 => {person,3742,6258,...},
   9221 => {person,9221,...},
   4969 => {person,...},
   9846 => {...},...}}
4>
4> T1 / 1000.
16.92
5>


the create time is 19ms, and update time is 17ms
the escript running time is 7ms and 5 ms.

Why in the erl shell, there is slower than escript?



So,  when I run my erlang application via:  erl -pa xxx -s myapp start
-detached .
Is it as slow as running with erl shell ?










-- 
My GitHub
https://github.com/yueyoum
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160928/50aca697/attachment.htm>


More information about the erlang-questions mailing list