From 53695d7948967f04b943cc59fab9ec144ce925ad Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Wed, 21 Jun 2023 16:17:52 +1000 Subject: [PATCH] More benchmarks --- inputs/passing/{fib.ts => fib25.ts} | 0 inputs/passing/helpers/quickSort.ts | 58 +++++++++++++++++++ inputs/passing/helpers/randish.ts | 12 ++++ inputs/passing/prime1000.ts | 7 +++ inputs/passing/projEuler/p7.ts | 2 +- inputs/passing/quickSort5000.ts | 17 ++++++ .../files/root/examples/quickSort.ts | 9 +-- 7 files changed, 98 insertions(+), 7 deletions(-) rename inputs/passing/{fib.ts => fib25.ts} (100%) create mode 100644 inputs/passing/helpers/quickSort.ts create mode 100644 inputs/passing/helpers/randish.ts create mode 100644 inputs/passing/prime1000.ts create mode 100644 inputs/passing/quickSort5000.ts diff --git a/inputs/passing/fib.ts b/inputs/passing/fib25.ts similarity index 100% rename from inputs/passing/fib.ts rename to inputs/passing/fib25.ts diff --git a/inputs/passing/helpers/quickSort.ts b/inputs/passing/helpers/quickSort.ts new file mode 100644 index 0000000..c476871 --- /dev/null +++ b/inputs/passing/helpers/quickSort.ts @@ -0,0 +1,58 @@ +export default function quickSort(vals: T[], cmp: (a: T, b: T) => number) { + // Demonstrates the ability to do in-place updates in ValueScript. + // + // There's only one reference to `vals`, so we can mutate it in-place + // without violating value semantics. + // + // More on quickSort: + // https://www.youtube.com/watch?v=Hoixgm4-P4M + + const len = vals.length; + let ranges: [number, number][] = [[0, len - 1]]; + + while (true) { + const range = ranges.shift(); + + if (!range) { + return vals; + } + + const [start, end] = range; + + if (end - start <= 0) { + continue; + } + + let i = start; + let j = end; + + let pivotIndex = Math.floor((i + j) / 2); + [vals[pivotIndex], vals[j]] = [vals[j], vals[pivotIndex]]; + const pivot = vals[j]; + j--; + + while (true) { + while (cmp(vals[i], pivot) < 0) { + i++; + } + + while (cmp(vals[j], pivot) > 0) { + j--; + } + + if (i < j) { + [vals[i], vals[j]] = [vals[j], vals[i]]; + i++; + j--; + continue; + } + + [vals[i], vals[end]] = [vals[end], vals[i]]; + + ranges.push([start, i - 1]); + ranges.push([i + 1, end]); + + break; + } + } +} diff --git a/inputs/passing/helpers/randish.ts b/inputs/passing/helpers/randish.ts new file mode 100644 index 0000000..8a55ae1 --- /dev/null +++ b/inputs/passing/helpers/randish.ts @@ -0,0 +1,12 @@ +export default function* randish() { + let x = 0.123; + + while (true) { + x -= (x * x + 1) / (2 * x + 0.1); + x -= (x * x + 1) / (2 * x + 0.1); + x -= (x * x + 1) / (2 * x + 0.1); + + const y = 1000 * x; + yield y - Math.floor(y); + } +} diff --git a/inputs/passing/prime1000.ts b/inputs/passing/prime1000.ts new file mode 100644 index 0000000..837795c --- /dev/null +++ b/inputs/passing/prime1000.ts @@ -0,0 +1,7 @@ +//! bench() + +import { Range_primes } from "./helpers/range.ts"; + +export default function main() { + return Range_primes().at(999); +} diff --git a/inputs/passing/projEuler/p7.ts b/inputs/passing/projEuler/p7.ts index cfd6a44..1320c0b 100644 --- a/inputs/passing/projEuler/p7.ts +++ b/inputs/passing/projEuler/p7.ts @@ -1,5 +1,5 @@ import { Range_primes } from "../helpers/range.ts"; export default function main() { - return Range_primes().at(10_000); + return Range_primes().at(1_000); } diff --git a/inputs/passing/quickSort5000.ts b/inputs/passing/quickSort5000.ts new file mode 100644 index 0000000..17c2039 --- /dev/null +++ b/inputs/passing/quickSort5000.ts @@ -0,0 +1,17 @@ +//! bench() + +import quickSort from "./helpers/quickSort.ts"; +import randish from "./helpers/randish.ts"; +import { Range_from } from "./helpers/range.ts"; + +export default function main() { + let nums = [ + ...Range_from(randish()) + .map((x) => Math.floor(5_000 * x)) + .limit(5_000), + ]; + + nums = quickSort(nums, (a, b) => a - b); + + return nums; +} diff --git a/website/src/playground/files/root/examples/quickSort.ts b/website/src/playground/files/root/examples/quickSort.ts index 3e4a714..e6b65ba 100644 --- a/website/src/playground/files/root/examples/quickSort.ts +++ b/website/src/playground/files/root/examples/quickSort.ts @@ -8,12 +8,9 @@ function quickSort(vals: T[], cmp: (a: T, b: T) => number) { // Demonstrates the ability to do in-place updates in ValueScript. // // There's only one reference to `vals`, so we can mutate it in-place - // without violating value semantics. - // - // (At the time of writing the internals aren't very careful about - // minimizing ref counters so this might not be happening, but the logic - // to mutate in-place when the ref count is one is already there. This - // will be optimized/fixed in the future.) + // without violating value semantics. (Disclaimer: ValueScript doesn't + // yet know that it can drop the reference to `x` from the call site, + // so there is one copy.) // // More on quickSort: // https://www.youtube.com/watch?v=Hoixgm4-P4M