-
Notifications
You must be signed in to change notification settings - Fork 2
/
nghashmap.bench.prw
194 lines (161 loc) · 5.41 KB
/
nghashmap.bench.prw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 NG Informática - TOTVS Software Partner
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Benchmarks using {NGHashMap}, {tHashMap} and array storage with {aScan}
*/
#include 'protheus.ch'
#define BENCHMARK_DIR '\benchmark'
#define CSV_ASCAN 'ascan.csv'
#define CSV_NGHASHMAP 'nghashmap.csv'
#define CSV_THASHMAP 'thashmap.csv'
#define PUT 'put-'
#define GET 'get-'
Static Function PrepareCSV( cFile )
Local nHandle
If !ExistDir( BENCHMARK_DIR )
MakeDir( BENCHMARK_DIR )
EndIf
If File( BENCHMARK_DIR + '\' + cFile )
FErase( BENCHMARK_DIR + '\' + cFile )
EndIf
nHandle := FCreate( BENCHMARK_DIR + '\' + cFile )
Return nHandle
Static Function Report( nHandle, nStart, nCount )
FWrite( nHandle, cValToChar( Seconds() - nStart ) + ',' + cValToChar( nCount ) + Chr( 10 ) )
Return
Static Function NextWord()
Local nLength := Randomize( 4, 12 )
Local nIndex
Local cWord := ''
For nIndex := 1 To nLength
cWord += Chr( Randomize( 33, 90 ) )
Next
Return cWord
Static Function GenData( aPairs )
Local nIndex
Local cWord
For nIndex := 1 To 10000
cWord := NextWord()
aPairs[ nIndex ] := { cWord, cWord }
Next
Return
Static Function PutNGHASH( nHandle, aData )
Local oMap
Local nIndex
Local nRet := 0
Local nStart
oMap := NGHashMap():New( 15000 )
For nIndex := 1 To Len( aData )
nStart := Seconds()
oMap:Put( aData[ nIndex, 1 ], aData[ nIndex, 2 ] )
Report( nHandle, nStart, ++nRet )
Next
Return oMap
Static Function GetNGHASH( nHandle, aData, oMap )
Local nIndex
Local nRet := 0
Local nStart
For nIndex := 1 To oMap:Length()
nStart := Seconds()
oMap:Get( aData[ nIndex, 1 ] )
Report( nHandle, nStart, ++nRet )
Next
Return
Static Function PutASCAN( nHandle, aData )
Local aValues := {}
Local nIndex
Local nRet := 0
Local nStart
For nIndex := 1 To Len( aData )
nStart := Seconds()
aAdd( aValues, { aData[ nIndex, 1 ], aData[ nIndex, 2 ] } )
Report( nHandle, nStart, ++nRet )
Next
Return aValues
Static Function GetASCAN( nHandle, aData, aMap )
Local nIndex
Local nRet := 0
Local nStart
For nIndex := 1 To Len( aMap )
nStart := Seconds()
aScan( aMap, { |aVal| aVal[ 1 ] == aData[ nIndex, 1 ] } )
Report( nHandle, nStart, ++nRet )
Next
Return
Static Function PutTHASH( nHandle, aData )
Local oMap
Local nIndex
Local nRet := 0
Local nStart
oMap := HMNew()
For nIndex := 1 To Len( aData )
nStart := Seconds()
HMSet( oMap, aData[ nIndex, 1 ], aData[ nIndex, 2 ] )
Report( nHandle, nStart, ++nRet )
Next
Return oMap
Static Function GetTHASH( nHandle, aData, oMap )
Local nIndex
Local aList := {}
Local nRet := 0
Local xValue
Local nStart
HMList( oMap, @aList )
For nIndex := 1 To Len( aList )
nStart := Seconds()
HMGet( oMap, aData[ nIndex, 1 ], @xValue )
Report( nHandle, nStart, ++nRet )
Next
Return
User Function NGHMBench()
Local nHandleNGHASH := PrepareCSV( PUT + CSV_NGHASHMAP )
Local nHandleASCAN := PrepareCSV( PUT + CSV_ASCAN )
Local nHandleTHASH := PrepareCSV( PUT + CSV_THASHMAP )
Local aPairs[ 10000 ]
Local oNGValues
Local oTValues
Local aValues
GenData( @aPairs )
oNGValues := PutNGHASH( nHandleNGHASH, @aPairs )
oTValues := PutTHASH( nHandleTHASH, @aPairs )
aValues := PutASCAN( nHandleASCAN, @aPairs )
FClose( nHandleASCAN )
FClose( nHandleNGHASH )
FClose( nHandleTHASH )
nHandleNGHASH := PrepareCSV( GET + CSV_NGHASHMAP )
nHandleASCAN := PrepareCSV( GET + CSV_ASCAN )
nHandleTHASH := PrepareCSV( GET + CSV_THASHMAP )
GetNGHASH( nHandleNGHASH, @aPairs, oNGValues )
GetTHASH( nHandleTHASH, @aPairs, oTValues )
GetASCAN( nHandleASCAN, @aPairs, aValues )
FClose( nHandleASCAN )
FClose( nHandleNGHASH )
FClose( nHandleTHASH )
CpyS2T( BENCHMARK_DIR + '\' + PUT + CSV_NGHASHMAP, 'C:\tmp/' )
CpyS2T( BENCHMARK_DIR + '\' + PUT + CSV_ASCAN, 'C:\tmp/' )
CpyS2T( BENCHMARK_DIR + '\' + PUT + CSV_THASHMAP, 'C:\tmp/' )
CpyS2T( BENCHMARK_DIR + '\' + GET + CSV_NGHASHMAP, 'C:\tmp/' )
CpyS2T( BENCHMARK_DIR + '\' + GET + CSV_ASCAN, 'C:\tmp/' )
CpyS2T( BENCHMARK_DIR + '\' + GET + CSV_THASHMAP, 'C:\tmp/' )
Return