Skip to content

Commit

Permalink
Added performer
Browse files Browse the repository at this point in the history
  • Loading branch information
sweko committed Dec 6, 2024
1 parent dffcc53 commit c1f75a0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 2024/ts/day-06/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readInputLines, readInput } from "../system/aoc-helper";
import "../utils/array-helpers";
import { Puzzle } from "../model/puzzle";
import { printMatrix } from "../utils/matrix";
import { Performancer } from "../utils/performancer";

type Input = {
maze: boolean[][]; // true is open, false is wall
Expand Down Expand Up @@ -120,7 +121,7 @@ const partOne = (input: Input, debug: boolean) => {
const stateToString = ({ position, direction }: State) => `${positionToString(position)}-${direction}`;

const willItLoop = (maze: boolean[][], position: Position, direction: Direction) => {

Performancer.start("willItLoop");
const visiteds = new Set<string>();
const width = maze[0].length;
const height = maze.length;
Expand All @@ -136,13 +137,15 @@ const willItLoop = (maze: boolean[][], position: Position, direction: Direction)

// check if we go overboard
if (next.x < 0 || next.x >= width || next.y < 0 || next.y >= height) {
Performancer.end("willItLoop");
return false;
}

if (maze[next.y][next.x]) {
state.position = next;
const stateString = stateToString(state);
if (visiteds.has(stateString)) {
Performancer.end("willItLoop");
return true;
}
visiteds.add(stateString);
Expand Down Expand Up @@ -175,6 +178,8 @@ const partTwo = (input: Input, debug: boolean) => {
}
}

Performancer.print("willItLoop");

return positions;
};

Expand Down
101 changes: 101 additions & 0 deletions 2024/ts/utils/performancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
export type PerformancerInterval = {
readonly start: number;
readonly end: number;
readonly duration: number;
}

export type PerformancerData = {
intervals: PerformancerInterval[];
name: string;
get totalDuration(): number;
get count(): number;
}

const createPerformancerItem = (name: string): PerformancerData => {
const intervals: PerformancerInterval[] = [];
return {
intervals,
name,
get totalDuration() {
return intervals.reduce((acc, { duration }) => acc + duration, 0);
},
get count() {
return intervals.length;
}
};
};

export type Performancer = {
readonly start: (name: string) => void;
readonly end: (name: string) => void;
readonly get: (name: string) => PerformancerData | undefined;
readonly printAll: (printDetails?: boolean) => void;
readonly print: (name: string, printDetails?: boolean) => void;
}

const toThreeDecimals = (value: number) => Math.round(value * 1000) / 1000;

const createPerformancer = (): Performancer => {
const items: Map<string, PerformancerData> = new Map();

const start = (name: string) => {
if (!items.has(name)) {
items.set(name, createPerformancerItem(name));
}
const start = performance.now();
items.get(name)!.intervals.push({ start, end: 0, duration: 0 });
};

const end = (name: string) => {
const end = performance.now();
const item = items.get(name);
if (!item) {
throw new Error("No item found");
}
const interval = item.intervals.pop();
if (!interval) {
throw new Error("No interval found");
}
const duration = toThreeDecimals(end - interval.start);
const finishedInterval = { ...interval, end, duration };
item.intervals.push(finishedInterval);
};

const get = (name: string) => items.get(name);

const printAll = (printDetails: boolean = false) => {
for (const name of Object.keys(items)) {
print(name, printDetails);
console.log("-----------------");
}
};

const print = (name: string, printDetails: boolean = false) => {
const item = items.get(name);
if (!item) {
console.log(`No performance data for ${name}`);
return;
}
console.log(`Performance for ${name}:`);
if (printDetails) {
for (const interval of item.intervals) {
console.log(` - ${interval.duration}ms`);
}
}
console.log(`${item.count} total calls`);
const totalDuration = toThreeDecimals(item.totalDuration);
console.log(`Total duration: ${totalDuration}ms`);
console.log(`Average duration: ${toThreeDecimals(totalDuration / item.count)}ms`);
}

return {
start,
end,
get,
printAll,
print
};
};


export const Performancer = createPerformancer();

0 comments on commit c1f75a0

Please sign in to comment.