Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-08

sluggish Trunc

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

I've made a little test 4x using this code in both pas2js and dwscript/sms:

-------
  try
    for I := 0 to 999999999 do
    begin
      Aux := Trunc(I / 2);
    end;
  finally
    console.log(Aux);
  end;
-------

======+|================
pas2js => time elapsed

9.78179999999702 secs
8.77317499998026 secs
8.65989000000991 secs
8.4647399999667 secs

pas2js => time elapsed

1.9587950001005083 secs
1.3956900000339374 secs
1.3954049999592826 secs
1.3983049999224022 secs

The sluggish is the pas2js Trunc function, if I replace to this dwscript version, I have similar results!

//-----------dwscript----------------------------
this.Trunc = function(v) { return (v>=0)?Math.floor(v):Math.ceil(v) };
//-------pas2js ---------------
  this.Trunc = function (A) {
    if (!Math.trunc) {
      Math.trunc = function(v) {
        v = +v;
        if (!isFinite(v)) return v;
        return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
      };
    }
    $mod.Trunc = Math.trunc;
    return Math.trunc(A);
  };
-------------------------------

Reply by email