-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.ts
74 lines (72 loc) · 3.01 KB
/
benchmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { ackermann_bigint, ackermann_string, ackermann_simple } from "./ackermann.ts";
console.log("Now benchmarking Ackermann-Pro - developed by JustAnotherJavaProgrammer");
const n = parseInt(Deno.args[0], 10);
const m = parseInt(Deno.args[1], 10);
const logging = Deno.args.includes("-l") || Deno.args.includes("--logging");
const skipRecursion = Deno.args.includes("-s") || Deno.args.includes("--skip-recursion");
if (Deno.args.length < 2 || Number.isNaN(n) || Number.isNaN(m)) {
console.info("Usage: benchmark [n] [m] OPTIONS\nValid options are:\n--logging, -l\tenable logging of intermediate steps\n--skip-recursion, -s\tSkip the recursion method");
Deno.exit(1);
}
if (Deno.args[0].includes(".") || Deno.args[1].includes(".") || n < 0 || m < 0) {
console.error("ERROR: The ackermann function only accepts positive integers.");
Deno.exit(2);
}
console.log(`Calculating the value of a(${n}, ${m}) using the ${skipRecursion ? "two" : "three"} different methods...`);
if (!skipRecursion) {
console.log("Using recursion...");
if (logging)
console.warn("WARN: The recursive implementation does not support the logging option.");
performance.mark("startr");
try {
ackermann_simple(n, m);
performance.mark("endr");
performance.measure("recursion", { start: "startr", end: "endr" });
} catch (e) {
console.error(e);
}
performance.clearMarks();
}
console.log("Using Strings...");
{
performance.mark("starts");
ackermann_string(n, m, logging);
performance.mark("ends");
performance.measure("string", { start: "starts", end: "ends" });
performance.clearMarks();
}
let value = -1n;
console.log("Using BigInts...");
{
const bn = BigInt(n);
const bm = BigInt(m);
performance.mark("startb");
value = ackermann_bigint(bn, bm, logging);
performance.mark("endb");
performance.measure("bigint", { start: "startb", end: "endb" });
performance.clearMarks();
}
console.log(`The results are in!\na(${n}, ${m}) = ${value !== -1n ? value.toString() : "Error during calculation"}`);
const timeSpentColumn = ["Time spent (ms)"];
const methodColumn = ["Method"];
const measurements = performance.getEntriesByType("measure");
measurements.sort((a, b) => {
return a.duration - b.duration;
});
for (const measurement of measurements) {
timeSpentColumn.push(measurement.duration.toString());
methodColumn.push(measurement.name === "recursion" ? "Recursion" :
(measurement.name === "string" ? "Strings" :
(measurement.name === "bigint" ? "BigInts" : "Unknown")));
}
// Padding the left column to get a consistently indented right column
let longestLength = timeSpentColumn[0].length;
// Finding the item with the longest length
for (const item of timeSpentColumn) {
if (item.length > longestLength)
longestLength = item.length;
}
// Padding all items to the longest length and printing them
for (let i = 0; i < timeSpentColumn.length; i++) {
console.log(`${timeSpentColumn[i].padEnd(longestLength)}\t${methodColumn[i]}`);
}