1+ using FriendlyCSharp . Databases ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Diagnostics ;
5+
6+ namespace DuplicityKeys . Core . sample
7+ {
8+ class Program
9+ {
10+ static int CmpBtnKey ( BtnKey keyX , BtnKey keyY , object objCmp )
11+ {
12+ // return < 0 (less), = 0 (equal), > 0 (greater)
13+ int iResult = String . Compare ( keyX . keyCity , keyY . keyCity , StringComparison . Ordinal ) ;
14+ if ( iResult == 0 )
15+ {
16+ iResult = keyX . keyId . CompareTo ( keyY . keyId ) ;
17+ //if (iResult == 0)
18+ //{
19+ // iResult = DateTime.Compare(key0, keyY.key0);
20+ // if (iResult == 0)
21+ // {
22+ // iResult = string.Compare(key1, keyY.key1, true);
23+ // if (iResult == 0)
24+ // {
25+ // iResult = key2.CompareTo(keyY.key2);
26+ // }
27+ // }
28+ //}
29+ }
30+ return iResult ;
31+ }
32+ //
33+ static bool FuncUpdate ( BtnKey keyAdd , ref BtnKey keyUpdate , object objUpdate )
34+ {
35+ // Resize ?
36+ if ( keyUpdate . aValueDateTime . Length <= keyUpdate . valueCount )
37+ {
38+ if ( keyUpdate . aValueDateTime . Length >= 1024 )
39+ Array . Resize < DateTime > ( ref keyUpdate . aValueDateTime , keyUpdate . aValueDateTime . Length + 256 ) ;
40+ else
41+ Array . Resize < DateTime > ( ref keyUpdate . aValueDateTime , keyUpdate . aValueDateTime . Length * 2 ) ;
42+ }
43+ // Resize ?
44+ if ( keyUpdate . aValueRand . Length <= keyUpdate . valueCount )
45+ {
46+ if ( keyUpdate . aValueRand . Length >= 1024 )
47+ Array . Resize < uint > ( ref keyUpdate . aValueRand , keyUpdate . aValueRand . Length + 256 ) ;
48+ else
49+ Array . Resize < uint > ( ref keyUpdate . aValueRand , keyUpdate . aValueRand . Length * 2 ) ;
50+ }
51+ // Update
52+ keyUpdate . aValueDateTime [ keyUpdate . valueCount ] = keyAdd . aValueDateTime [ 0 ] ;
53+ keyUpdate . aValueRand [ keyUpdate . valueCount ] = keyAdd . aValueRand [ 0 ] ;
54+ keyUpdate . valueCount ++ ;
55+ return true ;
56+ }
57+ //
58+ private struct BtnKey
59+ {
60+ public string keyCity ;
61+ public long keyId ;
62+ //public DateTime key0;
63+ //public string key1;
64+ //public long key2;
65+ public int valueCount ;
66+ public DateTime [ ] aValueDateTime ;
67+ public uint [ ] aValueRand ;
68+ }
69+ //
70+ //
71+ //
72+ static void Main ( string [ ] args )
73+ {
74+ Console . OutputEncoding = System . Text . Encoding . UTF8 ;
75+ Console . WriteLine ( String . Format ( "DuplicityKeys.Core20.sample, {0}" , ( IntPtr . Size == 4 ) ? "32 bit" : "64 bit" ) ) ;
76+ Console . WriteLine ( "-----------------------------------" ) ;
77+
78+ int max = 36000 ;
79+ string [ ] aCity = { "London" , "Moscow" , "Warsaw" , "Berlin" , "Paris" , "Prague" , "Brussels" , "Vienna" , "Zagreb" , "Helsinki" } ;
80+ int iPocetAdd = 0 ;
81+ Random r = new Random ( ( int ) DateTime . Now . Ticks ) ;
82+ uint [ ] aRand = new uint [ max ] ;
83+ var hashSetRand = new HashSet < uint > ( ) ;
84+ while ( iPocetAdd < max )
85+ {
86+ uint rand = ( uint ) r . Next ( 0 , Int32 . MaxValue - 1 ) ;
87+ if ( hashSetRand . Contains ( rand ) == false )
88+ {
89+ hashSetRand . Add ( rand ) ;
90+ aRand [ iPocetAdd ] = rand ;
91+ iPocetAdd ++ ;
92+ }
93+ }
94+ hashSetRand = null ;
95+ long iMem = GC . GetTotalMemory ( true ) ;
96+ Console . WriteLine ( "UsedMemory {0,4} MB" , iMem >> 20 ) ;
97+ long iMemOld = iMem ;
98+ //
99+ //
100+ iPocetAdd = 0 ;
101+ Console . WriteLine ( "-----------------------------------" ) ;
102+ Console . WriteLine ( "FcsFastBTreeN" ) ;
103+ BtnKey key ;
104+ key . aValueRand = new uint [ 1 ] ;
105+ key . aValueDateTime = new DateTime [ 1 ] ;
106+ FcsKeyFastBTreeN < BtnKey > btnTest = new FcsKeyFastBTreeN < BtnKey > ( CmpBtnKey , FuncUpdate , 32 ) ;
107+ var swFcsKV = Stopwatch . StartNew ( ) ;
108+ for ( int iter = 0 ; iter < 100 ; iter ++ )
109+ {
110+ for ( int idx = 0 ; idx < max ; idx ++ )
111+ {
112+ key . keyCity = aCity [ idx % aCity . Length ] ;
113+ key . keyId = idx % 1200 ;
114+ key . valueCount = 1 ;
115+ key . aValueRand [ 0 ] = aRand [ idx ] ;
116+ key . aValueDateTime [ 0 ] = DateTime . Now ;
117+ if ( btnTest . BtnAdd ( key ) != null )
118+ iPocetAdd ++ ;
119+ }
120+ }
121+ swFcsKV . Stop ( ) ;
122+ iMem = GC . GetTotalMemory ( true ) ;
123+ 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 ,
124+ btnTest . BtnUsedKeys ( ) , ( ( double ) ( swFcsKV . Elapsed . TotalMilliseconds * 1000000 ) / iPocetAdd ) , iPocetAdd ) ;
125+ iMemOld = iMem ;
126+ int iCompareCount = 0 ;
127+ swFcsKV . Restart ( ) ;
128+ foreach ( BtnKey ? value in btnTest )
129+ iCompareCount ++ ;
130+ swFcsKV . Stop ( ) ;
131+ Console . WriteLine ( "\n FcsKeyFastBTreeN - foreach()" ) ;
132+ 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") ;
133+
134+ iCompareCount = 0 ;
135+ FcsKeyFastBTreeN < BtnKey > . BtnKeyEnumeratorFast btnEn = btnTest . GetEnumeratorFastEx ( false ) ;
136+ swFcsKV . Restart ( ) ;
137+ while ( btnEn . MoveNext ( ) )
138+ {
139+ BtnKey ? value = btnEn . Current ;
140+ iCompareCount ++ ;
141+ }
142+ swFcsKV . Stop ( ) ;
143+ Console . WriteLine ( "\n FcsKeyFastBTreeN - foreach()" ) ;
144+ 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") ;
145+
146+ iCompareCount = 0 ;
147+ FcsKeyFastBTreeN < BtnKey > . BtnFastKey btnFast ;
148+ swFcsKV . Restart ( ) ;
149+ if ( btnTest . BtnFastFirst ( out BtnKey fcsKey2 , out btnFast ) != null )
150+ {
151+ iCompareCount ++ ;
152+ while ( btnTest . BtnFastNext ( ref fcsKey2 , ref btnFast ) != null )
153+ iCompareCount ++ ;
154+ }
155+ swFcsKV . Stop ( ) ;
156+ btnFast . Dispose ( ) ;
157+ Console . WriteLine ( "\n FcsFastBTreeN - BtnFastFirst()/BtnFastNext()" ) ;
158+ 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") ;
159+ //
160+ //
161+ Console . WriteLine ( "-----------------------------------" ) ;
162+ Console . WriteLine ( "Key ENTER press." ) ;
163+ Console . ReadLine ( ) ;
164+ }
165+ }
166+ }
0 commit comments