-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
242 lines (237 loc) · 8.18 KB
/
Program.cs
File metadata and controls
242 lines (237 loc) · 8.18 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.Diagnostics;
using FriendlyCSharp.Databases;
namespace MultipleKeys.Multi.sample
{
class Program
{
private class TestKV : FcsFastBTreeN<BtnKey, BtnValue>
{
protected override bool BtnUpdates(BtnKey keyAdd, BtnValue valueAdd, ref BtnValue valueUpdates, object objUpdates)
{
// Resize ?
if (valueUpdates.aDateTime.Length <= valueUpdates.count)
{
if (valueUpdates.aDateTime.Length >= 1024)
Array.Resize<DateTime>(ref valueUpdates.aDateTime, valueUpdates.aDateTime.Length + 256);
else
Array.Resize<DateTime>(ref valueUpdates.aDateTime, valueUpdates.aDateTime.Length * 2);
}
// Resize ?
if (valueUpdates.aRand.Length <= valueUpdates.count)
{
if (valueUpdates.aRand.Length >= 1024)
Array.Resize<uint>(ref valueUpdates.aRand, valueUpdates.aRand.Length + 256);
else
Array.Resize<uint>(ref valueUpdates.aRand, valueUpdates.aRand.Length * 2);
}
// Update
if (valueAdd.aDateTime == null)
valueUpdates.aDateTime[valueUpdates.count] = DateTime.MinValue;
else
valueUpdates.aDateTime[valueUpdates.count] = valueAdd.aDateTime[0];
// Update
if (valueAdd.aRand == null)
valueUpdates.aRand[valueUpdates.count] = uint.MaxValue;
else
valueUpdates.aRand[valueUpdates.count] = valueAdd.aRand[0];
valueUpdates.count++;
return true;
}
//////////////////////////
protected override void BtnAddFirst(BtnValue valueIn, out BtnValue valueAdd)
{
if (valueIn.count != 1)
{
valueIn.count = 1;
valueIn.aDateTime = new DateTime[1];
valueIn.aRand = new uint[1];
}
if (valueIn.aDateTime == null)
{
valueIn.aDateTime = new DateTime[1];
valueIn.aDateTime[0] = DateTime.MinValue;
}
if (valueIn.aRand == null)
{
valueIn.aRand = new uint[1];
valueIn.aRand[0] = uint.MaxValue;
}
base.BtnAddFirst(valueIn, out valueAdd);
}
//////////////////////////
private void _BtnUsedValues(ref uint Count, KeyValuePage QQ)
{
if (QQ != null)
{
if (QQ.kvPageNextRight != null)
{
_BtnUsedValues(ref Count, QQ.kvPageNextRight[0]);
for (int II = 1; II <= QQ.iDataCount; II++)
{
_BtnUsedValues(ref Count, QQ.kvPageNextRight[II]);
Count += (uint)QQ.aData[II].value.count;
}
}
else
{
for (int II = 1; II <= QQ.iDataCount; II++)
Count += (uint)QQ.aData[II].value.count;
}
}
}
//////////////////////////
public uint BtnUsedValues()
{
uint Count = 0;
_BtnUsedValues(ref Count, _btnRoot);
return Count;
}
//////////////////////////
public TestKV() : base(32, 32)
{
}
}
//
private struct BtnKey : IComparable<BtnKey>
{
public string keyCity;
public long keyId;
//public DateTime key0;
//public string key1;
//public long key2;
public int CompareTo(BtnKey keyY)
{
// return < 0 (less), = 0 (equal), > 0 (greater)
int iResult = String.Compare(keyCity, keyY.keyCity, StringComparison.Ordinal);
if (iResult == 0)
{
iResult = keyId.CompareTo(keyY.keyId);
//if (iResult == 0)
//{
// iResult = DateTime.Compare(key0, keyY.key0);
// if (iResult == 0)
// {
// iResult = string.Compare(key1, keyY.key1, true);
// if (iResult == 0)
// {
// iResult = key2.CompareTo(keyY.key2);
// }
// }
//}
}
return iResult;
}
}
//
private struct BtnValue
{
public int count;
public DateTime[] aDateTime;
public uint[] aRand;
}
//
//
//
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(String.Format("MultipleKeys.Multi.sample, {0}", (IntPtr.Size == 4) ? "32 bit" : "64 bit"));
Console.WriteLine("----------------------------------");
int max = 36000;
string[] aCity = { "London", "Moscow", "Warsaw", "Berlin", "Paris", "Prague", "Brussels", "Vienna", "Zagreb", "Helsinki" };
int iPocetAdd = 0;
Random r = new Random((int)DateTime.Now.Ticks);
uint[] aRand = new uint[max];
var hashSetRand = new HashSet<uint>();
while (iPocetAdd < max)
{
uint rand = (uint)r.Next(0, Int32.MaxValue - 1);
if (hashSetRand.Contains(rand) == false)
{
hashSetRand.Add(rand);
aRand[iPocetAdd] = rand;
iPocetAdd++;
}
}
hashSetRand = null;
long iMem = GC.GetTotalMemory(true);
Console.WriteLine("UsedMemory {0,4} MB", iMem >> 20);
long iMemOld = iMem;
//
//
iPocetAdd = 0;
Console.WriteLine("----------------------------------");
Console.WriteLine("FcsFastBTreeN");
TestKV btnTest = new TestKV();
var swFcsKV = Stopwatch.StartNew();
for (int iter = 0; iter < 100; iter++)
{
for (int idx = 0; idx < max; idx++)
{
BtnKey key;
key.keyCity = aCity[idx % aCity.Length];
key.keyId = idx % 1200;
BtnValue value;
value.count = 1;
if (idx % 50 == 12)
value.aRand = null;
else
{
value.aRand = new uint[1];
value.aRand[0] = aRand[idx];
}
if (idx % 60 == 12)
value.aDateTime = null;
else
{
value.aDateTime = new DateTime[1];
value.aDateTime[0] = DateTime.Now;
}
if (btnTest.BtnAdd(key, ref value, null) != null)
iPocetAdd++;
}
}
swFcsKV.Stop();
iMem = GC.GetTotalMemory(true);
Console.WriteLine("UsedMemory {0,5} MB [{1,5:N1} s] | {3} keys | Δ {2,3} MB | {4,8:N0} ns | {5,10:N0} values", iMem >> 20, swFcsKV.Elapsed.TotalSeconds, (iMem - iMemOld) >> 20,
btnTest.BtnUsedKeys(), ((double)(swFcsKV.Elapsed.TotalMilliseconds * 1000000) / iPocetAdd), btnTest.BtnUsedValues());
iMemOld = iMem;
int iCompareCount = 0;
swFcsKV.Restart();
foreach (KeyValuePair<BtnKey, BtnValue>? value in btnTest)
iCompareCount++;
swFcsKV.Stop();
Console.WriteLine("\nFcsFastBTreeN - foreach()");
Console.WriteLine($"{((double)(swFcsKV.Elapsed.TotalMilliseconds * 1000000) / iCompareCount),9:N2} ns [{swFcsKV.Elapsed.TotalMilliseconds,11} ms | {iCompareCount} keys ]{iCompareCount / swFcsKV.Elapsed.TotalSeconds,20:N0} IOPS");
iCompareCount = 0;
FcsBTreeN<BtnKey, BtnValue>.BtnEnumerator btnEn = btnTest.GetEnumeratorEx(false);
swFcsKV.Restart();
while (btnEn.MoveNext())
{
KeyValuePair<BtnKey, BtnValue>? value = btnEn.Current;
iCompareCount++;
}
swFcsKV.Stop();
Console.WriteLine("\nFcsBTreeN - foreach()");
Console.WriteLine($"{((double)(swFcsKV.Elapsed.TotalMilliseconds * 1000000) / iCompareCount),9:N2} ns [{swFcsKV.Elapsed.TotalMilliseconds,11} ms | {iCompareCount} keys ]{iCompareCount / swFcsKV.Elapsed.TotalSeconds,20:N0} IOPS");
iCompareCount = 0;
swFcsKV.Restart();
if (btnTest.BtnFastFirst(out BtnKey fcsKey2, out BtnValue fcsValue2, 2) != null)
{
iCompareCount++;
while (btnTest.BtnFastNext(ref fcsKey2, out fcsValue2, 2) != null)
iCompareCount++;
}
swFcsKV.Stop();
Console.WriteLine("\nFcsFastBTreeN - BtnFastFirst()/BtnFastNext()");
Console.WriteLine($"{((double)(swFcsKV.Elapsed.TotalMilliseconds * 1000000) / iCompareCount),9:N2} ns [{swFcsKV.Elapsed.TotalMilliseconds,11} ms | {iCompareCount} keys ]{iCompareCount / swFcsKV.Elapsed.TotalSeconds,20:N0} IOPS");
//
//
Console.WriteLine("----------------------------------");
Console.WriteLine("Key ENTER press.");
Console.ReadLine();
}
}
}