22using System ;
33using System . Collections . Generic ;
44using System . Diagnostics ;
5+ using System . Runtime . InteropServices ;
56
67namespace DuplicityKeys . Core . sample
78{
89 class Program
910 {
1011 //
12+ [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
1113 private struct BtnKey
1214 {
1315 public string keyCity ;
@@ -45,20 +47,18 @@ static int CmpBtnKey(BtnKey keyX, BtnKey keyY, object objCmp)
4547 static bool FuncUpdate ( BtnKey keyAdd , ref BtnKey keyUpdate , object objUpdate )
4648 {
4749 // Resize ?
48- if ( keyUpdate . aValueDateTime . Length <= keyUpdate . valueCount )
50+ if ( ( keyUpdate . aValueDateTime . Length <= keyUpdate . valueCount ) || ( keyUpdate . aValueRand . Length <= keyUpdate . valueCount ) )
4951 {
50- if ( keyUpdate . aValueDateTime . Length >= 1024 )
51- Array . Resize < DateTime > ( ref keyUpdate . aValueDateTime , keyUpdate . aValueDateTime . Length + 256 ) ;
52+ if ( ( keyUpdate . aValueDateTime . Length >= 1024 ) || ( keyUpdate . aValueRand . Length >= 1024 ) )
53+ {
54+ Array . Resize < DateTime > ( ref keyUpdate . aValueDateTime , keyUpdate . aValueDateTime . Length + 1024 ) ;
55+ Array . Resize < uint > ( ref keyUpdate . aValueRand , keyUpdate . aValueRand . Length + 1024 ) ;
56+ }
5257 else
58+ {
5359 Array . Resize < DateTime > ( ref keyUpdate . aValueDateTime , keyUpdate . aValueDateTime . Length * 2 ) ;
54- }
55- // Resize ?
56- if ( keyUpdate . aValueRand . Length <= keyUpdate . valueCount )
57- {
58- if ( keyUpdate . aValueRand . Length >= 1024 )
59- Array . Resize < uint > ( ref keyUpdate . aValueRand , keyUpdate . aValueRand . Length + 256 ) ;
60- else
6160 Array . Resize < uint > ( ref keyUpdate . aValueRand , keyUpdate . aValueRand . Length * 2 ) ;
61+ }
6262 }
6363 // Update
6464 keyUpdate . aValueDateTime [ keyUpdate . valueCount ] = keyAdd . aValueDateTime [ 0 ] ;
@@ -69,7 +69,7 @@ static bool FuncUpdate(BtnKey keyAdd, ref BtnKey keyUpdate, object objUpdate)
6969 //
7070 public static FcsKeyFastBTreeN < BtnKey > CreateFcsKeyFastBTreeN ( )
7171 {
72- return new FcsKeyFastBTreeN < BtnKey > ( BtnKey . CmpBtnKey , BtnKey . FuncUpdate , 32 ) ;
72+ return new FcsKeyFastBTreeN < BtnKey > ( CmpBtnKey , FuncUpdate , 32 ) ;
7373 }
7474 }
7575 //
@@ -126,12 +126,17 @@ static void Main(string[] args)
126126 }
127127 swFcsKV . Stop ( ) ;
128128 iMem = GC . GetTotalMemory ( true ) ;
129- 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 ,
130- btnTest . BtnUsedKeys ( ) , ( ( double ) ( swFcsKV . Elapsed . TotalMilliseconds * 1000000 ) / iPocetAdd ) , iPocetAdd ) ;
129+ Console . WriteLine ( "UsedMemory {0,5} MB [{1,5:N1} s] | {3} keys | Δ {2,3} MB | {4,8:N0} ns | {5,10:N0} values | sizeT {6,2} Byte " , iMem >> 20 , swFcsKV . Elapsed . TotalSeconds , ( iMem - iMemOld ) >> 20 ,
130+ btnTest . BtnUsedKeys ( ) , ( ( double ) ( swFcsKV . Elapsed . TotalMilliseconds * 1000000 ) / iPocetAdd ) , iPocetAdd , Marshal . SizeOf < BtnKey > ( ) ) ;
131131 iMemOld = iMem ;
132132 int iCompareCount = 0 ;
133+ // generate code at run time
134+ foreach ( BtnKey ? value in btnTest )
135+ iCompareCount ++ ;
136+ //
137+ iCompareCount = 0 ;
133138 swFcsKV . Restart ( ) ;
134- foreach ( BtnKey ? value in btnTest )
139+ foreach ( BtnKey ? value in btnTest )
135140 iCompareCount ++ ;
136141 swFcsKV . Stop ( ) ;
137142 Console . WriteLine ( "\n FcsKeyFastBTreeN - foreach()" ) ;
@@ -140,12 +145,13 @@ static void Main(string[] args)
140145 iCompareCount = 0 ;
141146 FcsKeyFastBTreeN < BtnKey > . BtnKeyEnumeratorFast btnEn = btnTest . GetEnumeratorFastEx ( false ) ;
142147 swFcsKV . Restart ( ) ;
143- while ( btnEn . MoveNext ( ) )
148+ while ( btnEn . MoveNext ( ) )
144149 {
145150 BtnKey ? value = btnEn . Current ;
146151 iCompareCount ++ ;
147152 }
148153 swFcsKV . Stop ( ) ;
154+ btnEn . Dispose ( ) ;
149155 Console . WriteLine ( "\n FcsKeyFastBTreeN - foreach()" ) ;
150156 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") ;
151157
0 commit comments