Skip to content

Instantly share code, notes, and snippets.

@be5invis
Last active September 13, 2023 20:32
Show Gist options
  • Save be5invis/7d30338511c112a10f49f981a0bd5004 to your computer and use it in GitHub Desktop.
Save be5invis/7d30338511c112a10f49f981a0bd5004 to your computer and use it in GitHub Desktop.
Haskell-ish AltJS performance test results

Candidates

  • JavaScript: Hand-written JavaScript.

  • Idris-codegen-es: Idris 1.0 with idris-codegen-es.

  • Idris (Official JS): Idris 1.0 with official JavaScript Codegen.

  • PureScript: Using Latest PureScript.

  • PureScript (Uncurried): Latest PureScript with Data.Function.Uncurry.

  • Idris (C): Idris 1.0 with C codegen on Windows x64.

  • C: Hand-written JavaScript, compiled with gcc 6.2.0, with Win64 settings and -O3.

Testing Method

All JavaScript files have been wrapped to add performance counter using process.hrtime. Due to V8's optimization strategy, all tests are run in 20 times and all results are shown.

All Tests are run on:

  • Node.js v6.9.5 LTS;
  • Windows 10 RS2 (15063) x64;
  • Dell workstation with Intel Xeon E5-2609 v3.

Performance Results (in milliseconds)

JavaScript Idris-codegen-es Idris (Official JS) PureScript PureScript (Uncurried) Idris (C) C
13.73339506 13.42742282 1704.505935 200.855022 22.124794 94 5.578
14.78157305 13.43711933 1689.17194 193.658534 21.573255 78 5.562
14.68451508 13.36427394 1757.765448 192.537088 21.568393 94 5.5
14.40595136 13.35289202 1724.666578 217.592446 22.068075 109 5.485
14.22472414 13.36110299 1720.508691 192.888756 21.689937 109 5.484
14.20338641 13.32920984 1666.046162 192.978969 21.524637 94 5.594
13.4270619 13.3442705 1615.222931 208.516077 21.572175 110 5.547
13.5088298 13.52317791 1643.122418 193.317672 21.539222 78 5.531
13.92866314 13.40879147 1603.685972 220.824436 21.506811 109 5.531
13.88535464 13.43780537 1506.79053 215.318221 21.523557 109 5.5
14.14017507 13.39441686 1509.226273 196.178007 21.492226 79 5.547
14.68206731 13.55266719 1580.30144 208.429646 21.50465 93 5.516
14.903307 13.35848305 1478.445922 213.409169 21.493306 125 5.515
14.84051769 13.43006437 1633.779733 191.941793 21.484663 94 5.547
14.01911638 13.39064089 1598.434188 201.801984 21.564072 94 5.547
14.5271873 13.56396808 1620.066866 217.814466 21.521936 109 5.547
14.46749568 13.47575952 1631.17707 205.041538 21.546245 110 5.469
13.77878838 13.39785791 1560.412516 222.862054 21.510052 93 5.5
13.86227816 13.53480292 1686.185198 210.021062 21.488984 110 5.484
14.1432812 13.41552771 1631.03986 202.36919 21.514914 125 5.531
module Main
tarai : Int -> Int -> Int -> Int
tarai x y z =
if x <= y
then y
else tarai (tarai (x-1) y z) (tarai (y-1) z x) (tarai (z-1) x y)
main : JS_IO ()
main = do
putStr' $ show (tarai 11 5 0)
// Tarai function, manually implemented
var tarai = function (x, y, z) {
if (x <= y) {
return y;
} else {
return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y));
}
};
// Tarai function, generated using Idris-codegen-is
function tarai_idris(_123_e__0_125_, _123_e__1_125_, _123_e__2_125_) {
for (; ;) {
if ((_123_e__0_125_ <= _123_e__1_125_)) {
return _123_e__1_125_;
} else {
const cgIdris_2 = tarai_idris((_123_e__0_125_ - 1), _123_e__1_125_, _123_e__2_125_);
const cgIdris_3 = tarai_idris((_123_e__1_125_ - 1), _123_e__2_125_, _123_e__0_125_);
const cgIdris_4 = tarai_idris((_123_e__2_125_ - 1), _123_e__0_125_, _123_e__1_125_);
_123_e__0_125_ = cgIdris_2;
_123_e__1_125_ = cgIdris_3;
_123_e__2_125_ = cgIdris_4;
continue;
}
break;
}
}
// Too long
// See: https://gist.github.com/be5invis/dd4a0e54fd6ed9bb0ad084688688a350
// Tarai function, generated using Purescript
var tarai_purs = function (x) {
return function (y) {
return function (z) {
var $1 = x <= y;
if ($1) {
return y;
};
return tarai_purs(tarai_purs(x - 1 | 0)(y)(z))(tarai_purs(y - 1 | 0)(z)(x))(tarai_purs(z - 1 | 0)(x)(y));
};
};
};
import Prelude
import Data.Function.Uncurried
tarai = mkFn3 \x y z ->
if x <= y then y else runFn3 tarai (runFn3 tarai (x - 1) y z) (runFn3 tarai (y - 1) z x) (runFn3 tarai (z - 1) x y)
var tarai = function (x, y, z) {
var $0 = x <= y;
if ($0) {
return y;
};
return tarai(tarai(x - 1 | 0, y, z), tarai(y - 1 | 0, z, x), tarai(z - 1 | 0, x, y));
};
module Main
import Data.Bits
%include C "Windows.h"
||| Compile under x64 only
getTickCount : IO Int
getTickCount = foreign FFI_C "GetTickCount64" (IO Int)
tarai : Int -> Int -> Int -> Int
tarai x y z = if x <= y
then y
else tarai (tarai (x - 1) y z) (tarai (y - 1) z x) (tarai (z - 1) x y)
main : IO ()
main = do
for [1..20] $ \_ => do
before <- getTickCount
pure $ show (tarai 11 5 0)
after <- getTickCount
printLn (after - before)
pure ()
#include <stdio.h>
#include <stdint.h>
#include <Windows.h>
int tarai(int x, int y, int z)
{
if (x <= y)
{
return y;
}
else
{
return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y));
}
}
int main()
{
for (int j = 0; j < 20; j++)
{
uint64_t start = GetTickCount64();
int a = 0;
for (int repeat = 0; repeat < 1000; repeat++)
{
a += tarai(11, 5, 0);
}
uint64_t end = GetTickCount64();
printf("%d\t%d\n", a, end - start);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment