From b65afe682498d41d7eee70b3840716197f3c1b20 Mon Sep 17 00:00:00 2001 From: Andrei Alexeev Date: Thu, 6 Apr 2023 20:07:43 +0200 Subject: [PATCH] feature/time-measurement-decorator --- decorators.ts | 16 ++++++++++++++++ graph.ts | 12 +++++++++--- main.ts | 3 +++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 decorators.ts diff --git a/decorators.ts b/decorators.ts new file mode 100644 index 0000000..19a3490 --- /dev/null +++ b/decorators.ts @@ -0,0 +1,16 @@ +import { verbose } from "./main.ts"; + +export function measureTime() { + return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { + const original: (...args: Array) => unknown = descriptor.value; + + const timeLabel: string = `${target.constructor.name}.${propertyKey}`; + descriptor.value = function (...args: Array) { + verbose && console.time(timeLabel); + const value: unknown = original.apply(this, args); + verbose && console.timeEnd(timeLabel); + + return value; + }; + }; +} diff --git a/graph.ts b/graph.ts index dfe3954..6c01315 100644 --- a/graph.ts +++ b/graph.ts @@ -1,5 +1,8 @@ import { format } from "duration"; import { colors } from "cliffy"; +import { measureTime } from "./decorators.ts"; + + export class Graph { size: number; nodes: number[][]; @@ -32,18 +35,16 @@ export class Graph { this.verbose && this.logTime("Graph built in", start, end); } + get subGraphs() { const visited = new Uint8Array(this.size); const subgraphs: number[][] = []; - const start = performance.now(); for (let i = 0; i < this.size; i++) { if (!visited[i]) { visited[i] = 1; subgraphs.push(this.dfs(i, visited)); } } - const end = performance.now(); - this.verbose && this.logTime("Subgraphs created in", start, end); return subgraphs; } @@ -70,4 +71,9 @@ export class Graph { )}` ); } + + @measureTime() + printTimeAfterFunction(){ + return this.subGraphs; + } } diff --git a/main.ts b/main.ts index 8437d0f..c070711 100644 --- a/main.ts +++ b/main.ts @@ -22,6 +22,7 @@ while (true) { Select.separator("---------"), { name: "Size of subgraphs", value: "subgraphsSize" }, { name: "List of subgraphs", value: "subgraphsNodes" }, + { name: "Measure Time with decorator", value: "measureTime"} ], }); switch (command) { @@ -43,5 +44,7 @@ while (true) { case "subgraphsNodes": console.log(graph.subGraphs); break; + case "measureTime": + graph.printTimeAfterFunction(); } }