[test] big o estimate refined

This commit is contained in:
Revone 2023-10-21 14:46:41 +08:00
parent 39baccd17c
commit cb56b04af5
4 changed files with 34 additions and 32 deletions

View file

@ -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)

4
package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -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<string, [number, number][] > = new Map();
export function logBigOMetricsWrap<F extends AnyFunction>(fn: F, args: Parameters<F>, 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<F extends AnyFunction>(fn: F, args: Parameters<F>, 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;
}