1- const INPUT_COUNT = 1e8 ;
1+ const INPUT_COUNT = 1e6 ;
22const INPUT_MAX = 1e6 ;
33const TEST_COUNT = 5 ;
44
5- function time ( ) {
6- return process . hrtime ( ) ;
7- } ;
8-
9- function performance ( start , end ) {
10- return ~ ~ ( ( ( end [ 0 ] * 1e9 + end [ 1 ] ) - ( start [ 0 ] * 1e9 + start [ 1 ] ) ) / 1e6 )
11- }
12-
135function Random ( max ) {
14- return ~ ~ ( Math . random ( ) * max ) ;
6+ return ~ ~ ( Math . random ( ) * max ) ;
157}
168
179function Random10 ( ) {
@@ -47,11 +39,10 @@ function BenchmarkTest(trees, testcount, inputcount, inputmax) {
4739 let results = { } ;
4840 trees . forEach ( ( tree , index ) => {
4941 tree . desc = tree . desc || index ;
50- results [ tree . desc ] = {
51- MIX_SUM : 0 ,
52- }
42+ results [ tree . desc ] = { }
5343 } ) ;
5444 const input = randomArray ( inputcount , inputmax ) ;
45+ console . log ( `benchmark start :${ inputcount } ${ inputmax } ` ) ;
5546 while ( testcount ) {
5647 const ops = randomOp ( inputcount ) ;
5748 for ( let n = 0 ; n < trees . length ; n ++ ) {
@@ -60,18 +51,17 @@ function BenchmarkTest(trees, testcount, inputcount, inputmax) {
6051 desc
6152 } = trees [ n ] ;
6253 let result = results [ desc ] ;
63- tree = new Tree ( )
54+ tree = new Tree ( ) ;
6455 // BEGIN: MIX
56+ console . time ( desc ) ;
6557 let {
66- cost,
67- height,
68- size
58+ height
6959 } = testOnce ( input , ops , Tree )
70- result [ testcount ] = cost + "(" + height + "," + size + ")" ;
71- // result[count] = cost;
72- result . MIX_SUM += cost ;
60+ console . timeEnd ( desc ) ;
61+ result [ testcount ] = height ;
7362 // END: MIX
7463 }
64+ console . table ( results ) ;
7565 testcount -- ;
7666 }
7767 return results ;
@@ -81,7 +71,6 @@ function BenchmarkTest(trees, testcount, inputcount, inputmax) {
8171function testOnce ( input , ops , Tree ) {
8272 tree = new Tree ( )
8373 // BEGIN: MIX
84- start = time ( ) ;
8574 for ( let j = 0 ; j < INPUT_COUNT ; j ++ ) {
8675 let op = ops [ j ] ;
8776 let value = input [ j ] ;
@@ -93,10 +82,7 @@ function testOnce(input, ops, Tree) {
9382 tree . insert ( value ) ;
9483 }
9584 }
96- end = time ( ) ;
97- cost = performance ( start , end ) ;
9885 return {
99- cost,
10086 height : tree . height ,
10187 size : tree . size
10288 }
@@ -134,7 +120,7 @@ class SetFakeTree {
134120// TEST:
135121let AVLTree = require ( "../../src/tree/AVLTree" ) ;
136122let BinarySearchTree = require ( "../../src/tree/BinarySearchTree" ) ;
137- let result = BenchmarkTest ( [ {
123+ let trees = [ {
138124 Tree : SetFakeTree ,
139125 desc : "Set"
140126} , {
@@ -143,5 +129,6 @@ let result = BenchmarkTest([{
143129} , {
144130 Tree : BinarySearchTree ,
145131 desc : 'BinarySearchTree'
146- } ] , TEST_COUNT , INPUT_COUNT , INPUT_MAX )
147- console . table ( result )
132+ } ]
133+
134+ BenchmarkTest ( trees , TEST_COUNT , INPUT_COUNT , INPUT_MAX )
0 commit comments