Benchmarking
This commit is contained in:
parent
0bbe734fbc
commit
7ca5b3d8fe
43
2021/1.ts
43
2021/1.ts
|
@ -1,4 +1,9 @@
|
|||
import { inputNumbers } from "./common.ts";
|
||||
import {
|
||||
collectArray,
|
||||
inputNumbers,
|
||||
measureDuration,
|
||||
preloadedAsyncIterator,
|
||||
} from "./common.ts";
|
||||
|
||||
/** This solution is relatively simple. Keep track of the last depth we
|
||||
* measured in `lastDepth` (skipping the first line of input by defaulting it
|
||||
|
@ -21,7 +26,28 @@ export async function part1(
|
|||
}
|
||||
return increases;
|
||||
}
|
||||
console.log("Part 1", await part1(await inputNumbers("1")));
|
||||
const input1 = await inputNumbers("1");
|
||||
await measureDuration(async () =>
|
||||
console.log("Part 1 Async Naive", await part1(input1))
|
||||
);
|
||||
const input1pre = await preloadedAsyncIterator(await inputNumbers("1"));
|
||||
await measureDuration(async () =>
|
||||
console.log("Part 1 Preloaded", await part1(input1pre))
|
||||
);
|
||||
const input1pre2 = await preloadedAsyncIterator(await inputNumbers("1"));
|
||||
await measureDuration(async () =>
|
||||
console.log("Part 1 via Part2's Method", await part2(input1pre2, 1))
|
||||
);
|
||||
|
||||
const input1pre3 = await collectArray(await inputNumbers("1"));
|
||||
const p1t = window.performance.now();
|
||||
let answer = 0;
|
||||
for (let i = 1; i <= input1pre3.length - 1; i++) {
|
||||
if (input1pre3[i] > input1pre3[i - 1]) answer++;
|
||||
}
|
||||
console.log("Part 1 Fast", answer);
|
||||
const p1t2 = window.performance.now();
|
||||
console.log(p1t2 - p1t, "ms");
|
||||
|
||||
/** Since the windows we need to compare overlap, we really only need to
|
||||
* compare the values unique to each window, which will be the first of the
|
||||
|
@ -45,4 +71,15 @@ export async function part2(
|
|||
}
|
||||
return increases;
|
||||
}
|
||||
console.log("Part 2", await part2(await inputNumbers("1")));
|
||||
const input2 = await inputNumbers("1");
|
||||
await measureDuration(async () => console.log("Part 2", await part2(input2)));
|
||||
|
||||
const input2f = await collectArray(await inputNumbers("1"));
|
||||
const p2t = window.performance.now();
|
||||
let answer2 = 0;
|
||||
for (let i = 2; i <= input2f.length - 1; i++) {
|
||||
if (input2f[i] > input2f[i - 3]) answer2++;
|
||||
}
|
||||
console.log("Part 2 Fast", answer2);
|
||||
const p2t2 = window.performance.now();
|
||||
console.log(p2t2 - p2t, "ms");
|
||||
|
|
|
@ -5,6 +5,7 @@ export const XDG_CACHE_HOME = Deno.env.get("XDG_CACHE_HOME") ||
|
|||
path.join(HOME, ".config");
|
||||
|
||||
const INPUT_CACHE_DIR = path.join(XDG_CACHE_HOME, "aoc2021");
|
||||
console.log(INPUT_CACHE_DIR);
|
||||
|
||||
async function fileExists(filePath: string) {
|
||||
try {
|
||||
|
@ -84,3 +85,30 @@ export async function* asyncIterator<T>(
|
|||
yield v;
|
||||
}
|
||||
}
|
||||
|
||||
export async function preloadedAsyncIterator<T>(
|
||||
iterator: AsyncIterableIterator<T>,
|
||||
): Promise<AsyncIterableIterator<T>> {
|
||||
const arr: T[] = [];
|
||||
for await (const v of iterator) {
|
||||
arr.push(v);
|
||||
}
|
||||
return asyncIterator(arr);
|
||||
}
|
||||
|
||||
export async function measureDuration(cb: () => Promise<void>) {
|
||||
const p1t = window.performance.now();
|
||||
await cb();
|
||||
const p1t2 = window.performance.now();
|
||||
console.log(p1t2 - p1t, "ms");
|
||||
}
|
||||
|
||||
export async function collectArray<T>(
|
||||
iterator: AsyncIterableIterator<T>,
|
||||
): Promise<T[]> {
|
||||
const arr: T[] = [];
|
||||
for await (const v of iterator) {
|
||||
arr.push(v);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
|
31
2021/one.nim
Normal file
31
2021/one.nim
Normal file
|
@ -0,0 +1,31 @@
|
|||
import std/[streams, strutils, sugar, strformat, times]
|
||||
|
||||
iterator inputForDay(day: int): int =
|
||||
var stream: FileStream = fmt"/home/daniel/.home/.config/aoc2021/{day}.input".openFileStream
|
||||
for s in stream.lines():
|
||||
yield s.parseInt
|
||||
|
||||
proc part1(inputs: seq[int], dist=1): int =
|
||||
result = 0
|
||||
var i = dist
|
||||
var x = inputs.len()-1
|
||||
while i <= x:
|
||||
if inputs[i] > inputs[i-dist]: inc result
|
||||
inc i
|
||||
|
||||
let dd1 = epochTime()
|
||||
var input = collect(newSeq):
|
||||
for i in inputForDay(1): i
|
||||
|
||||
let dd2 = epochTime()
|
||||
echo &"{(dd2 - dd1) * 1000} ms (to load input)"
|
||||
echo "Part 1"
|
||||
let d1 = epochTime()
|
||||
echo part1(input)
|
||||
let d2 = epochTime()
|
||||
echo &"{(d2 - d1) * 1000} ms (to calculate solution)"
|
||||
echo "Part 2"
|
||||
let d21 = epochTime()
|
||||
echo part1(input,3)
|
||||
let d22 = epochTime()
|
||||
echo &"{(d22 - d21) * 1000} ms (to calculate solution)"
|
Loading…
Reference in a new issue