1
- const INPUT_COUNT = 1e8 ;
1
+ const INPUT_COUNT = 1e6 ;
2
2
const INPUT_MAX = 1e6 ;
3
3
const TEST_COUNT = 5 ;
4
4
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
-
13
5
function Random ( max ) {
14
- return ~ ~ ( Math . random ( ) * max ) ;
6
+ return ~ ~ ( Math . random ( ) * max ) ;
15
7
}
16
8
17
9
function Random10 ( ) {
@@ -47,11 +39,10 @@ function BenchmarkTest(trees, testcount, inputcount, inputmax) {
47
39
let results = { } ;
48
40
trees . forEach ( ( tree , index ) => {
49
41
tree . desc = tree . desc || index ;
50
- results [ tree . desc ] = {
51
- MIX_SUM : 0 ,
52
- }
42
+ results [ tree . desc ] = { }
53
43
} ) ;
54
44
const input = randomArray ( inputcount , inputmax ) ;
45
+ console . log ( `benchmark start :${ inputcount } ${ inputmax } ` ) ;
55
46
while ( testcount ) {
56
47
const ops = randomOp ( inputcount ) ;
57
48
for ( let n = 0 ; n < trees . length ; n ++ ) {
@@ -60,29 +51,28 @@ function BenchmarkTest(trees, testcount, inputcount, inputmax) {
60
51
desc
61
52
} = trees [ n ] ;
62
53
let result = results [ desc ] ;
63
- tree = new Tree ( )
54
+ tree = new Tree ( ) ;
64
55
// BEGIN: MIX
56
+ console . time ( desc ) ;
65
57
let {
66
- cost,
67
- height,
68
- size
58
+ height
69
59
} = 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 ;
73
62
// END: MIX
74
63
}
64
+ console . table ( results ) ;
75
65
testcount -- ;
76
66
}
77
67
return results ;
78
68
}
79
69
80
70
81
71
function testOnce ( input , ops , Tree ) {
82
- tree = new Tree ( )
72
+ tree = new Tree ( ) ;
73
+ let length = input . length ;
83
74
// BEGIN: MIX
84
- start = time ( ) ;
85
- for ( let j = 0 ; j < INPUT_COUNT ; j ++ ) {
75
+ for ( let j = 0 ; j < length ; j ++ ) {
86
76
let op = ops [ j ] ;
87
77
let value = input [ j ] ;
88
78
if ( op === 1 ) {
@@ -93,10 +83,7 @@ function testOnce(input, ops, Tree) {
93
83
tree . insert ( value ) ;
94
84
}
95
85
}
96
- end = time ( ) ;
97
- cost = performance ( start , end ) ;
98
86
return {
99
- cost,
100
87
height : tree . height ,
101
88
size : tree . size
102
89
}
@@ -134,7 +121,7 @@ class SetFakeTree {
134
121
// TEST:
135
122
let AVLTree = require ( "../../src/tree/AVLTree" ) ;
136
123
let BinarySearchTree = require ( "../../src/tree/BinarySearchTree" ) ;
137
- let result = BenchmarkTest ( [ {
124
+ let trees = [ {
138
125
Tree : SetFakeTree ,
139
126
desc : "Set"
140
127
} , {
@@ -143,5 +130,7 @@ let result = BenchmarkTest([{
143
130
} , {
144
131
Tree : BinarySearchTree ,
145
132
desc : 'BinarySearchTree'
146
- } ] , TEST_COUNT , INPUT_COUNT , INPUT_MAX )
147
- console . table ( result )
133
+ } ]
134
+
135
+ BenchmarkTest ( trees , TEST_COUNT , INPUT_COUNT , INPUT_MAX )
136
+ // BenchmarkTest(trees, TEST_COUNT, INPUT_COUNT * 10, INPUT_MAX)
0 commit comments