Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine(String.Format("BtnEnumerator.Multi.sample, {0}", (IntPtr.Size == 4) ? "32 bit" : "64 bit"));
            Console.WriteLine("-----------------------------------");

            uint   uiCount = 1;
            TestKV btnTest = new TestKV();

            // Build the tree
            btnTest.BtnAdd(20, uiCount);
            btnTest.BtnAdd(40, uiCount);
            btnTest.BtnAdd(10, uiCount);
            btnTest.BtnAdd(30, uiCount);
            btnTest.BtnAdd(15, uiCount); //
            btnTest.BtnAdd(35, uiCount);
            btnTest.BtnAdd(7, uiCount);
            btnTest.BtnAdd(26, uiCount);
            btnTest.BtnAdd(18, uiCount);
            btnTest.BtnAdd(22, uiCount); //
            btnTest.BtnAdd(5, uiCount);  //
            btnTest.BtnAdd(42, uiCount);
            btnTest.BtnAdd(13, uiCount);
            btnTest.BtnAdd(46, uiCount);
            btnTest.BtnAdd(27, uiCount);
            btnTest.BtnAdd(27, uiCount); // duplicity call BtnUpdates()
            btnTest.BtnAdd(8, uiCount);
            btnTest.BtnAdd(32, uiCount); //
            btnTest.BtnAdd(38, uiCount);
            btnTest.BtnAdd(24, uiCount);
            btnTest.BtnAdd(27, uiCount); // duplicity call BtnUpdates()
            btnTest.BtnAdd(45, uiCount);
            btnTest.BtnAdd(25, uiCount); //

            // output: 5,7,8,10,13,15,18,20,22,24,25,26,27,30,32,35,38,40,42,45,46,
            foreach (KeyValuePair <int, uint>?keyValue in btnTest)
            {
                Console.Write(keyValue.GetValueOrDefault().Key + ",");
            }
            Console.WriteLine();

            // output: 5,7,8,10,13,15,18,20,22,24,25,26,
            FcsBTreeN <int, uint> .BtnEnumerator btnEn = btnTest.GetEnumeratorEx(false, 12);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            btnEn.Dispose();
            Console.WriteLine();

            int keyLo = 22;
            int keyHi = 38;

            // output: 5,7,8,10,13,15,18,20,22,24,25,26,27,30,32,35,38,
            btnEn = btnTest.GetEnumeratorEx(default(int), keyHi, false);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 22,24,25,26,27,30,32,35,38,40,42,45,46,
            btnEn = btnTest.GetEnumeratorEx(keyLo, default(int), false);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 22,24,25,26,27,30,32,35,38,
            btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, false);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            // output: 22,24,25,26,27,30,32,35,38,
            btnEn.Reset();
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 22,24,25,26,27,
            btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, false, 5);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // ---------- REVERSE ----------
            Console.WriteLine("-----------------------------------");
            // output: 46,45,42,40,38,35,32,30,27,26,25,24,22,20,18,15,13,10,8,7,5,
            btnEn = btnTest.GetEnumeratorEx(true);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 46,45,42,40,38,35,32,30,27,26,25,24,
            btnEn = btnTest.GetEnumeratorEx(true, 12);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 38,35,32,30,27,26,25,24,22,20,18,15,13,10,8,7,5,
            btnEn = btnTest.GetEnumeratorEx(default(int), keyHi, true);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 46,45,42,40,38,35,32,30,27,26,25,24,22,
            btnEn = btnTest.GetEnumeratorEx(keyLo, default(int), true);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 38,35,32,30,27,26,25,24,22,
            btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, true);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            // output: 38,35,32,30,27,
            btnEn = btnTest.GetEnumeratorEx(keyLo, keyHi, true, 5);
            while (btnEn.MoveNext())
            {
                Console.Write(btnEn.Current.Key + ",");
            }
            Console.WriteLine();
            btnEn.Dispose();

            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Key ENTER press.");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        //
        //
        //
        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();
        }