[erlang-questions] Erlang arithmetics

黃耀賢 (Yau-Hsien Huang) <>
Sun Oct 31 05:36:48 CET 2010


First, I'm sorry for an mistake in my previous message as "No recursion
used." Actually
I have a recursive function `fold' in JavaScript code, that made the
JavaScript version
slow.

Additional Information: when testing code like these, it's obviously
that using Node.js
won't overcome stack-overflow problem. It cannot beat some situation that
constructing
1,000,000 array elements, each contains two elements and one sub-array. In
data structure
JavaScript even with Node.js is weak.

--- Y-H. H.


2010/10/31 黃耀賢 (Yau-Hsien Huang) <>

> Hi, Dmitry. Your code in JavaScript with Node.js is not equivalent to
> the Erlang one. You could take some operation-equivalent program:
> for example, I modified your JavaScript code, and make it follow fashion
> of your Erlang code, as
>
> // ==================================
> // Modified Erlang code
>
> test() ->
>    Now = now(),
>    seq(1, 1000),
>    timer:now_diff(now(), Now).
>
> seq(M, N) ->
>     L = lists:seq(M, N),
>     [{X0,Y0}|Ps] =
> [ {random:uniform(1000),random:uniform(1000)} || _ <- L ],
>     {Total,_} =
> lists:foldr(fun move_to/2, {0, {X0,Y0}}, Ps),
>     Total.
>
> move_to({X, Y}, {Sum, {X0, Y0}}) ->
>    {Sum + math:sqrt((X - X0) * (X - X0) + (Y - Y0) * (Y - Y0)), {X, Y}}.
>
> // ====================================
> // Modified JavaScript code with Node.js,
> // following Erlang fashion
>
> function move_to(xy, sum, xy0) {
>     var result = [];
>     result[0] = {};
>     result[0] = sum +
> Math.sqrt(
>     (xy[0]-xy0[0])*(xy[0]-xy0[0])
>     + (xy[1]-xy0[1])*(xy[1]-xy0[1])
>         );
>     result[1] = {};
>     result[1] = xy;
>     return result;
> }
>
> function fold(f, base, xy0, list) {
>     if (list.length == 0) {
> var result = [];
>  result[0] = {};
> result[0] = base;
> result[1] = {};
>  result[1] = xy0;
> return result;
>     }
>     var p = list[list.length-1];
>     list.length = list.length-1;
>     var sum_xy = move_to(p, base, xy0);
>     return fold(f, sum_xy[0], sum_xy[1], list);
> }
>
> function list_seq(m, n) {
>     var result = [];
>     var i;
>     for (i=m; i<=n; i++) {
> result[i-m] = {};
> result[i-m] = m;
>     }
>     return result;
> }
>
> function list_rand(seq) {
>     var result = [];
>     for (var i in seq) {
> result[i] = {};
>  result[i] = [];
> result[i][0] = Math.floor(Math.random() * 1000 + 1);
> result[i][1] = Math.floor(Math.random() * 1000 + 1);
>     }
>     return result;
> }
>
> function seq(m, n) {
>     var list = list_seq(m, n);
>     var list1 = list_rand(list);
>     var head = list1[list1.length-1];
>     list1.length--;
>     var result = fold(move_to, 0, head, list1);
>     return result[0];
> }
>
> var start = (new Date()).valueOf();
> console.log(seq(1,1000));
> var end = (new Date()).valueOf();
> console.log(end-start);
> // ===============================
>
> In my computer with Ubuntu 10.10, the JavaScript program performed in 24~25
> milliseconds, and the Erlang one performs about 8.5 milliseconds. Result is
> meaningful.
> The JavaScript one used some functions, but no recursive functions. Almost
> the same
> operations were performed, and the Erlang works fine. It may be persuasive.
>
>
> --
>
> Best Regards,
>
> --- Y-H. H.
>
>
> On Sat, Oct 30, 2010 at 4:03 PM, Dmitry Demeshchuk <>wrote:
>
>> Greetings.
>>
>> I'm writing an article comparing Erlang and Node.js and I stumbled
>> upon the performance question.
>>
>> My initial goal was to compare some basic arithmetics speed, like the
>> total distance between randomly distributed points. So, I have written
>> the following code for Erlang:
>>
> <cut />
>
>> and the following code for Node.js:
>>
>> ======================================================
>>
>> var a = [];
>> for(var i = 0; i < 1000000; i++) {
>>    a[i] = {};
>>    a[i].x = Math.floor(Math.random() * 1000);
>>    a[i].y = Math.floor(Math.random() * 1000);
>> }
>>
>> var sum = 0;
>>
>> var start = (new Date()).valueOf();
>>
>> for(var i = 1; i < 1000000; i++) {
>>    var prev = a[i-1];
>>    sum += Math.sqrt((a[i].x - prev.x) * (a[i].x - prev.x) + (a[i].y -
>> prev.y) * (a[i].y - prev.y));
>> }
>>
>> var end = (new Date()).valueOf();
>>
>> console.log(end - start);
>>
>> ============================================
>>
>>


-- 

Best Regards.

--- Y-H. H.


More information about the erlang-questions mailing list