From c985317259211205b6445634898310589a67435e Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 11 Nov 2023 09:59:11 +0800 Subject: [PATCH] test: Added a switch to control whether to test the performance of competitors. --- README.md | 33 ++++++---- package.json | 2 +- test/config.ts | 1 + .../binary-tree/rb-tree.test.ts | 28 ++++---- .../data-structures/hash/hash-map.test.ts | 66 ++++++++++--------- .../linked-list/doubly-linked-list.test.ts | 17 +++-- .../priority-queue/priority-queue.test.ts | 8 ++- .../data-structures/queue/deque.test.ts | 17 +++-- .../data-structures/queue/queue.test.ts | 17 +++-- .../data-structures/stack/stack.test.ts | 29 ++++---- 10 files changed, 129 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index 9f977bb..d626833 100644 --- a/README.md +++ b/README.md @@ -728,40 +728,49 @@ optimal approach to data structure design. [//]: # (No deletion!!! Start of Replace Section)
avl-tree
-
test nametime taken (ms)executions per secsample deviation
10,000 add randomly32.2331.030.00
10,000 add & delete randomly72.1913.850.00
10,000 addMany41.3924.169.33e-4
10,000 get28.0535.660.00
+
test nametime taken (ms)executions per secsample deviation
10,000 add randomly31.3431.903.63e-4
10,000 add & delete randomly71.6813.950.00
10,000 addMany48.6320.560.01
10,000 get28.9934.490.00
binary-tree
-
test nametime taken (ms)executions per secsample deviation
1,000 add randomly12.5679.596.46e-4
1,000 add & delete randomly16.1561.917.18e-4
1,000 addMany10.6493.956.17e-4
1,000 get18.4454.224.23e-4
1,000 dfs160.176.240.01
1,000 bfs57.4117.420.00
1,000 morris264.543.780.00
+
test nametime taken (ms)executions per secsample deviation
1,000 add randomly12.4780.212.46e-4
1,000 add & delete randomly16.2961.390.00
1,000 addMany10.3496.671.78e-4
1,000 get22.7244.020.01
1,000 dfs167.135.980.01
1,000 bfs57.0617.534.63e-4
1,000 morris272.803.670.01
bst
-
test nametime taken (ms)executions per secsample deviation
10,000 add randomly28.4535.154.14e-4
10,000 add & delete randomly67.7814.750.00
10,000 addMany29.0434.435.76e-4
10,000 get28.6734.888.26e-4
+
test nametime taken (ms)executions per secsample deviation
10,000 add randomly30.2833.024.41e-4
10,000 add & delete randomly73.9313.530.01
10,000 addMany29.6633.720.00
10,000 get27.9635.763.35e-4
rb-tree
-
test nametime taken (ms)executions per secsample deviation
100,000 add randomly90.9610.990.01
100,000 competitor add randomly50.1019.960.01
100,000 add & delete randomly219.064.560.01
100,000 getNode106.069.430.00
+
test nametime taken (ms)executions per secsample deviation
100,000 add randomly89.2611.200.01
100,000 add & delete randomly218.904.570.01
100,000 getNode41.7423.960.00
directed-graph
-
test nametime taken (ms)executions per secsample deviation
1,000 addVertex0.119433.924.40e-6
1,000 addEdge6.24160.302.02e-4
1,000 getVertex0.052.13e+41.64e-6
1,000 getEdge23.8042.020.00
tarjan218.464.580.01
tarjan all221.954.510.00
topologicalSort183.705.440.01
+
test nametime taken (ms)executions per secsample deviation
1,000 addVertex0.109995.321.43e-6
1,000 addEdge6.32158.287.89e-4
1,000 getVertex0.052.17e+42.66e-7
1,000 getEdge23.6842.230.00
tarjan214.154.670.01
tarjan all214.514.660.00
topologicalSort182.645.480.01
+
+
hash-map
+
test nametime taken (ms)executions per secsample deviation
10,000 set16.5160.579.87e-4
10,000 set & get34.7528.776.11e-4
heap
-
test nametime taken (ms)executions per secsample deviation
10,000 add & pop4.63215.974.50e-5
10,000 fib add & pop359.982.780.00
+
test nametime taken (ms)executions per secsample deviation
10,000 add & pop4.62216.344.19e-5
10,000 fib add & pop358.772.790.00
doubly-linked-list
-
test nametime taken (ms)executions per secsample deviation
1,000,000 unshift233.684.280.06
1,000,000 unshift & shift185.635.390.05
1,000,000 insertBefore321.193.110.04
+
test nametime taken (ms)executions per secsample deviation
1,000,000 unshift232.434.300.09
1,000,000 unshift & shift174.595.730.05
1,000,000 insertBefore322.713.100.07
singly-linked-list
-
test nametime taken (ms)executions per secsample deviation
10,000 push & pop217.954.590.01
10,000 insertBefore248.814.020.00
+
test nametime taken (ms)executions per secsample deviation
10,000 push & pop216.354.620.01
10,000 insertBefore246.914.050.00
max-priority-queue
-
test nametime taken (ms)executions per secsample deviation
10,000 refill & poll11.7285.302.79e-4
+
test nametime taken (ms)executions per secsample deviation
10,000 refill & poll11.6785.712.89e-4
+
+
priority-queue
+
test nametime taken (ms)executions per secsample deviation
10,000 add & pop12.4380.441.15e-4
deque
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push204.134.900.04
1,000,000 shift25.7138.890.00
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push222.164.500.06
1,000,000 shift26.3337.980.00
queue
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push42.9323.300.00
1,000,000 push & shift80.1212.480.00
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push45.9921.740.01
1,000,000 push & shift80.4912.420.00
+
+
stack
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push44.0422.710.01
1,000,000 push & pop50.0619.980.01
trie
-
test nametime taken (ms)executions per secsample deviation
100,000 push56.0917.830.00
100,000 getWords97.7610.230.01
+
test nametime taken (ms)executions per secsample deviation
100,000 push44.8722.297.28e-4
100,000 getWords88.4511.310.00
[//]: # (No deletion!!! End of Replace Section) \ No newline at end of file diff --git a/package.json b/package.json index ca7ae2a..bfbe76a 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "install:all-subs": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multimap-typed trie-typed undirected-graph-typed queue-typed --save-dev", "test": "jest --runInBand", "test:integration": "npm run update:subs && jest --config jest.integration.config.js", - "benchmark": "ts-node test/performance/reportor.ts", + "test:perf": "ts-node test/performance/reportor.ts", "check:deps": "dependency-cruiser src", "changelog": "auto-changelog", "coverage:badge": "istanbul-badges-readme", diff --git a/test/config.ts b/test/config.ts index 6ca1161..fc676fe 100644 --- a/test/config.ts +++ b/test/config.ts @@ -1 +1,2 @@ export const isDebugTest = false; +export const isCompetitor = false; diff --git a/test/performance/data-structures/binary-tree/rb-tree.test.ts b/test/performance/data-structures/binary-tree/rb-tree.test.ts index 2eb72f9..7e4d60c 100644 --- a/test/performance/data-structures/binary-tree/rb-tree.test.ts +++ b/test/performance/data-structures/binary-tree/rb-tree.test.ts @@ -2,6 +2,7 @@ import {RedBlackTree} from '../../../../src'; import * as Benchmark from 'benchmark'; import {getRandomIntArray, magnitude} from '../../../utils'; import {OrderedMap} from 'js-sdsl'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); const rbTree = new RedBlackTree(); @@ -15,21 +16,26 @@ suite for (let i = 0; i < arr.length; i++) { rbTree.add(arr[i]); } - }) - .add(`${HUNDRED_THOUSAND.toLocaleString()} competitor add randomly`, () => { + }); + + +if (isCompetitor) { + suite.add(`${HUNDRED_THOUSAND.toLocaleString()} competitor add randomly`, () => { for (let i = 0; i < arr.length; i++) { competitor.setElement(arr[i], arr[i]); } }) - .add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete randomly`, () => { - rbTree.clear(); - for (let i = 0; i < arr.length; i++) { - rbTree.add(arr[i]); - } - for (let i = 0; i < arr.length; i++) { - rbTree.delete(arr[i]); - } - }) +} + +suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & delete randomly`, () => { + rbTree.clear(); + for (let i = 0; i < arr.length; i++) { + rbTree.add(arr[i]); + } + for (let i = 0; i < arr.length; i++) { + rbTree.delete(arr[i]); + } +}) .add(`${HUNDRED_THOUSAND.toLocaleString()} getNode`, () => { for (let i = 0; i < arr.length; i++) { rbTree.getNode(arr[i]); diff --git a/test/performance/data-structures/hash/hash-map.test.ts b/test/performance/data-structures/hash/hash-map.test.ts index 703b76f..e9f1c91 100644 --- a/test/performance/data-structures/hash/hash-map.test.ts +++ b/test/performance/data-structures/hash/hash-map.test.ts @@ -2,44 +2,50 @@ import {HashMap} from '../../../../src'; import {HashMap as CHashMap} from 'js-sdsl'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); -const {LINEAR} = magnitude; +const {TEN_THOUSAND} = magnitude; suite - .add(`${LINEAR.toLocaleString()} set`, () => { + .add(`${TEN_THOUSAND.toLocaleString()} set`, () => { const hm = new HashMap(); - for (let i = 0; i < LINEAR; i++) { + for (let i = 0; i < TEN_THOUSAND; i++) { hm.set(i, i); } - }) - .add(`${LINEAR.toLocaleString()} competitor set`, () => { - const hm = new CHashMap(); - - for (let i = 0; i < LINEAR; i++) { - hm.setElement(i, i); - } - }) - .add(`${LINEAR.toLocaleString()} set & get`, () => { - const hm = new HashMap(); - - for (let i = 0; i < LINEAR; i++) { - hm.set(i, i); - } - for (let i = 0; i < LINEAR; i++) { - hm.get(i); - } - }) - .add(`${LINEAR.toLocaleString()} competitor set & get`, () => { - const hm = new CHashMap(); - - for (let i = 0; i < LINEAR; i++) { - hm.setElement(i, i); - } - for (let i = 0; i < LINEAR; i++) { - hm.getElementByKey(i); - } }); +if (isCompetitor) { + suite.add(`${TEN_THOUSAND.toLocaleString()} competitor set`, () => { + const hm = new CHashMap(); + + for (let i = 0; i < TEN_THOUSAND; i++) { + hm.setElement(i, i); + } + }) +} +suite.add(`${TEN_THOUSAND.toLocaleString()} set & get`, () => { + const hm = new HashMap(); + + for (let i = 0; i < TEN_THOUSAND; i++) { + hm.set(i, i); + } + for (let i = 0; i < TEN_THOUSAND; i++) { + hm.get(i); + } +}) +; +if (isCompetitor) { + suite.add(`${TEN_THOUSAND.toLocaleString()} competitor set & get`, () => { + const hm = new CHashMap(); + + for (let i = 0; i < TEN_THOUSAND; i++) { + hm.setElement(i, i); + } + for (let i = 0; i < TEN_THOUSAND; i++) { + hm.getElementByKey(i); + } + }) +} export {suite}; diff --git a/test/performance/data-structures/linked-list/doubly-linked-list.test.ts b/test/performance/data-structures/linked-list/doubly-linked-list.test.ts index 3911fd9..e25be19 100644 --- a/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +++ b/test/performance/data-structures/linked-list/doubly-linked-list.test.ts @@ -2,6 +2,7 @@ import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src'; import {LinkList as CLinkedList} from 'js-sdsl'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); const {LINEAR} = magnitude; @@ -14,14 +15,16 @@ suite list.unshift(i); } }) - .add(`${LINEAR.toLocaleString()} competitor unshift`, () => { - const list = new CLinkedList(); + if (isCompetitor) { + suite.add(`${LINEAR.toLocaleString()} competitor unshift`, () => { + const list = new CLinkedList(); - for (let i = 0; i < LINEAR; i++) { - list.pushFront(i); - } - }) - .add(`${LINEAR.toLocaleString()} unshift & shift`, () => { + for (let i = 0; i < LINEAR; i++) { + list.pushFront(i); + } + }) + } + suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => { const list = new DoublyLinkedList(); for (let i = 0; i < LINEAR; i++) { diff --git a/test/performance/data-structures/priority-queue/priority-queue.test.ts b/test/performance/data-structures/priority-queue/priority-queue.test.ts index 04808f2..b8f8693 100644 --- a/test/performance/data-structures/priority-queue/priority-queue.test.ts +++ b/test/performance/data-structures/priority-queue/priority-queue.test.ts @@ -2,6 +2,7 @@ import {PriorityQueue as CPriorityQueue} from 'js-sdsl'; import {PriorityQueue} from '../../../../src'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); const {TEN_THOUSAND} = magnitude; @@ -17,8 +18,9 @@ suite for (let i = 0; i < TEN_THOUSAND; i++) { pq.pop(); } - }) - .add(`${TEN_THOUSAND.toLocaleString()} competitor add & pop`, () => { + }); +if (isCompetitor) { + suite.add(`${TEN_THOUSAND.toLocaleString()} competitor add & pop`, () => { const pq = new CPriorityQueue(); for (let i = 0; i < TEN_THOUSAND; i++) { @@ -30,4 +32,6 @@ suite } }); +} + export {suite}; diff --git a/test/performance/data-structures/queue/deque.test.ts b/test/performance/data-structures/queue/deque.test.ts index a0bde90..3d6b774 100644 --- a/test/performance/data-structures/queue/deque.test.ts +++ b/test/performance/data-structures/queue/deque.test.ts @@ -2,6 +2,7 @@ import {Deque} from '../../../../src'; import {Deque as CDeque} from 'js-sdsl'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; export const suite = new Benchmark.Suite(); const {LINEAR} = magnitude; @@ -13,13 +14,15 @@ suite deque.push(i); } }) - .add(`${LINEAR.toLocaleString()} competitor push`, () => { - const deque = new CDeque(); - for (let i = 0; i < LINEAR; i++) { - deque.pushBack(i); - } - }) - .add(`${LINEAR.toLocaleString()} shift`, () => { + if (isCompetitor) { + suite.add(`${LINEAR.toLocaleString()} competitor push`, () => { + const deque = new CDeque(); + for (let i = 0; i < LINEAR; i++) { + deque.pushBack(i); + } + }) + } + suite.add(`${LINEAR.toLocaleString()} shift`, () => { const deque = new Deque(); for (let i = 0; i < LINEAR; i++) { deque.push(i); diff --git a/test/performance/data-structures/queue/queue.test.ts b/test/performance/data-structures/queue/queue.test.ts index 93e32a2..4e806a1 100644 --- a/test/performance/data-structures/queue/queue.test.ts +++ b/test/performance/data-structures/queue/queue.test.ts @@ -2,6 +2,7 @@ import {Queue} from '../../../../src'; import {Queue as CQueue} from 'js-sdsl'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); const {LINEAR} = magnitude; @@ -14,14 +15,16 @@ suite queue.push(i); } }) - .add(`${LINEAR.toLocaleString()} competitor push`, () => { - const queue = new CQueue(); + if (isCompetitor) { + suite.add(`${LINEAR.toLocaleString()} competitor push`, () => { + const queue = new CQueue(); - for (let i = 0; i < LINEAR; i++) { - queue.push(i); - } - }) - .add(`${LINEAR.toLocaleString()} push & shift`, () => { + for (let i = 0; i < LINEAR; i++) { + queue.push(i); + } + }) + } + suite.add(`${LINEAR.toLocaleString()} push & shift`, () => { const queue = new Queue(); for (let i = 0; i < LINEAR; i++) { diff --git a/test/performance/data-structures/stack/stack.test.ts b/test/performance/data-structures/stack/stack.test.ts index ec3c24d..0e4f89d 100644 --- a/test/performance/data-structures/stack/stack.test.ts +++ b/test/performance/data-structures/stack/stack.test.ts @@ -2,6 +2,7 @@ import {Stack} from '../../../../src'; import {Stack as CStack} from 'js-sdsl'; import * as Benchmark from 'benchmark'; import {magnitude} from '../../../utils'; +import {isCompetitor} from "../../../config"; const suite = new Benchmark.Suite(); const {LINEAR} = magnitude; @@ -14,24 +15,27 @@ suite stack.push(i); } }) - .add(`${LINEAR.toLocaleString()} competitor push`, () => { +if (isCompetitor) { + suite.add(`${LINEAR.toLocaleString()} competitor push`, () => { const queue = new CStack(); for (let i = 0; i < LINEAR; i++) { queue.push(i); } }) - .add(`${LINEAR.toLocaleString()} push & pop`, () => { - const queue = new Stack(); +} +suite.add(`${LINEAR.toLocaleString()} push & pop`, () => { + const queue = new Stack(); - for (let i = 0; i < LINEAR; i++) { - queue.push(i); - } - for (let i = 0; i < LINEAR; i++) { - queue.pop(); - } - }) - .add(`${LINEAR.toLocaleString()} competitor push & pop`, () => { + for (let i = 0; i < LINEAR; i++) { + queue.push(i); + } + for (let i = 0; i < LINEAR; i++) { + queue.pop(); + } +}) +if (isCompetitor) { + suite.add(`${LINEAR.toLocaleString()} competitor push & pop`, () => { const queue = new CStack(); for (let i = 0; i < LINEAR; i++) { @@ -40,6 +44,7 @@ suite for (let i = 0; i < LINEAR; i++) { queue.pop(); } - }); + }) +} export {suite};