From cb56b04af5073d61a03a23c9987247c218239d36 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 21 Oct 2023 14:46:41 +0800 Subject: [PATCH] [test] big o estimate refined --- CHANGELOG.md | 3 ++- package-lock.json | 4 ++-- package.json | 2 +- test/utils/big-o.ts | 57 +++++++++++++++++++++++---------------------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c65758..16c259d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,11 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.36.7](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.36.8](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes +- [heap] fibonacci heap implemented. [test] big O estimate. [project] n… [`#15`](https://github.com/zrwusa/data-structure-typed/pull/15) - [rbtree] implemented, but with bugs [`#13`](https://github.com/zrwusa/data-structure-typed/pull/13) - [trie] renamed ambiguous methods and add comments to all methods. [`#12`](https://github.com/zrwusa/data-structure-typed/pull/12) - [binarytree] modified the getDepth method to adhere to the proper def… [`#11`](https://github.com/zrwusa/data-structure-typed/pull/11) diff --git a/package-lock.json b/package-lock.json index f46acf4..72d4355 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.36.7", + "version": "1.36.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.36.7", + "version": "1.36.8", "license": "MIT", "devDependencies": { "@types/benchmark": "^2.1.3", diff --git a/package.json b/package.json index a0e641d..4e13f50 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "changelog": "auto-changelog", "coverage:badge": "istanbul-badges-readme", "ci": "env && npm run lint && npm run build && npm run update:individuals && npm run test && git fetch --tags && npm run changelog", - "publish:all": "npm run ci && npm publish && sh /scripts/publish_all_subs.sh && sh /scripts/publish_docs.sh" + "publish:all": "npm run ci && npm publish && sh scripts/publish_all_subs.sh && sh scripts/publish_docs.sh" }, "repository": { "type": "git", diff --git a/test/utils/big-o.ts b/test/utils/big-o.ts index 2f18aba..755bdb1 100644 --- a/test/utils/big-o.ts +++ b/test/utils/big-o.ts @@ -22,6 +22,7 @@ export const bigO = { FACTORIAL: 10000 }; + function findPotentialN(input: any): number { let longestArray: any[] = []; let mostProperties: { [key: string]: any } = {}; @@ -79,7 +80,7 @@ function linearRegression(x: number[], y: number[]) { function estimateBigO(runtimes: number[], dataSizes: number[]): string { // Make sure the input runtimes and data sizes have the same length if (runtimes.length !== dataSizes.length) { - return "输入数组的长度不匹配"; + return "Lengths of input arrays do not match"; } // Create an array to store the computational complexity of each data point @@ -131,6 +132,33 @@ function estimateBigO(runtimes: number[], dataSizes: number[]): string { const methodLogs: Map = new Map(); +export function logBigOMetricsWrap(fn: F, args: Parameters, fnName: string) { + const startTime = performance.now(); + const result = fn(args); + const endTime = performance.now(); + const runTime = endTime - startTime; + const methodName = `${fnName}`; + if (!methodLogs.has(methodName)) { + methodLogs.set(methodName, []); + } + + const methodLog = methodLogs.get(methodName); + + const maxDataSize = args.length === 1 && typeof args[0] === "number" ? args[0] : findPotentialN(args); + if (methodLog) { + methodLog.push([runTime, maxDataSize]); + + if (methodLog.length >= 20) { + console.log('triggered', methodName, methodLog); + const bigO = estimateBigO(methodLog.map(([runTime,]) => runTime), methodLog.map(([runTime,]) => runTime)); + console.log(`Estimated Big O: ${bigO}`); + methodLogs.delete(methodName) + } + } + + return result; +} + export function logBigOMetrics(target: any, propertyKey: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; @@ -164,30 +192,3 @@ export function logBigOMetrics(target: any, propertyKey: string, descriptor: Pro return descriptor; } - -export function logBigOMetricsWrap(fn: F, args: Parameters, fnName: string) { - const startTime = performance.now(); - const result = fn(args); - const endTime = performance.now(); - const runTime = endTime - startTime; - const methodName = `${fnName}`; - if (!methodLogs.has(methodName)) { - methodLogs.set(methodName, []); - } - - const methodLog = methodLogs.get(methodName); - - const maxDataSize = args.length === 1 && typeof args[0] === "number" ? args[0] : findPotentialN(args); - if (methodLog) { - methodLog.push([runTime, maxDataSize]); - - if (methodLog.length >= 20) { - console.log('triggered', methodName, methodLog); - const bigO = estimateBigO(methodLog.map(([runTime,]) => runTime), methodLog.map(([runTime,]) => runTime)); - console.log(`Estimated Big O: ${bigO}`); - methodLogs.delete(methodName) - } - } - - return result; -}