예제 #1
0
파일: Test.cs 프로젝트: DeusEx40K/HashTable
        public void RunProbe(IHashTable ht, Random rnd, int size)
        {
            var keysInsert  = KeyUnique(rnd, size);
            var keysInclude = new string[keysInsert.Length];

            int index = 0;

            for (int i = 0; i < keysInsert.Length; i++)
            {
                var result = ht.InsertProbe(keysInsert[i], keysInsert[i]);
                if (result.Result == 0)
                {
                    keysInclude[index] = keysInsert[i];
                    index++;
                }
                else
                {
                    Console.WriteLine($"Insert error {keysInsert[i]} = {result.Result}");
                }
            }

            Array.Resize(ref keysInclude, index);
            for (int i = 0; i < keysInclude.Length; i++)
            {
                var result = ht.SearchProbe(keysInclude[i]);
                if (result.Result != 0)
                {
                    Console.WriteLine($"Search include {keysInsert[i]} = {result.Result}");
                }
            }

            var keysExclude = KeyUnique(rnd, keysInclude);

            for (int i = 0; i < keysExclude.Length; i++)
            {
                var result = ht.SearchProbe(keysExclude[i]);
                if (result.Result == 0)
                {
                    Console.WriteLine($"Search exclude {keysInsert[i]} = {result.Result}");
                }
            }
        }
예제 #2
0
파일: Test.cs 프로젝트: DeusEx40K/HashTable
        public void RunTest(string TestName, string FileName, IHashTable ht, Random rnd, TestCase testcase)
        {
            using (var file = new StreamWriter(FileName, false, Encoding.UTF8))
            {
                totalTimer.Start();

                #region Header
                var line = testcase.ToStringFull() + $"{ht.Capacity}; " + $"{ht.Capacity * ht.EntrySize}; ";
                Console.WriteLine(line);
                file.WriteLine(line);

                var head = $"loadFactor; " +
                           $"{TestName}(вставка), кол.проб; " +
                           $"{TestName}(вставка), ср.кол.проб; " +
                           $"{TestName}(вставка), кол.тиков; " +
                           $"{TestName}(вставка), ср.кол.тиков; " +
                           $"{TestName}(успеш.поиск), кол.проб; " +
                           $"{TestName}(успеш.поиск), ср.кол.проб; " +
                           $"{TestName}(успеш.поиск), кол.тиков; " +
                           $"{TestName}(успеш.поиск), ср.кол.тиков; " +
                           $"{TestName}(неусп.поиск), кол.проб; " +
                           $"{TestName}(неусп.поиск), ср.кол.проб; " +
                           $"{TestName}(неусп.поиск), кол.тиков; " +
                           $"{TestName}(неусп.поиск), ср.кол.тиков; " +
                           $"";
                Console.WriteLine(head);
                file.WriteLine(head);
                file.Flush();
                #endregion

                for (int data = testcase.dataMin; data <= testcase.dataMax; data += testcase.dataStep)
                {
                    #region Initial
                    insertTimer.Reset();
                    includeTimer.Reset();
                    excludeTimer.Reset();
                    int insertProbe = 0, insertCount = 0;
                    int includeProbe = 0, includeCount = 0;
                    int excludeProbe = 0, excludeCount = 0;
                    GC.Collect();
                    #endregion

                    for (int test = 0; test < testcase.testCount; test++)
                    {
                        ht.Clear();

                        var keysInsert  = KeyUnique(rnd, data);
                        var keysInclude = new string[keysInsert.Length];

                        int index = 0;
                        for (int i = 0; i < keysInsert.Length; i++)
                        {
                            var result = ht.InsertProbe(keysInsert[i], keysInsert[i]);
                            if (result.Result == 0)
                            {
                                keysInclude[index] = keysInsert[i];
                                index++;
                                insertProbe += result.Probe;
                                insertCount++;
                            }
                            else
                            {
                                //throw new Exception($"Insert error {keysInsert[i]} = {result}");
                            }
                        }

                        Array.Resize(ref keysInclude, index);
                        for (int i = 0; i < keysInclude.Length; i++)
                        {
                            var result = ht.SearchProbe(keysInclude[i]);
                            if (result.Result != 0)
                            {
                                //throw new Exception($"Search include {keysInsert[i]} = {result}");
                            }
                            includeProbe += result.Probe;
                            includeCount++;
                        }

                        var keysExclude = KeyUnique(rnd, keysInclude);
                        for (int i = 0; i < keysExclude.Length; i++)
                        {
                            var result = ht.SearchProbe(keysExclude[i]);
                            if (result.Result == 0)
                            {
                                //throw new Exception($"Search exclude {keysInsert[i]} = {result}");
                            }
                            excludeProbe += result.Probe;
                            excludeCount++;
                        }

                        ht.Clear();

                        for (int i = 0; i < keysInclude.Length; i++)
                        {
                            Ext.FillCache(testcase.fillSize);
                            insertTimer.Start();
                            ht.Insert(keysInclude[i], keysInclude[i]);
                            insertTimer.Stop();
                        }

                        for (int i = 0; i < keysInclude.Length; i++)
                        {
                            Ext.FillCache(testcase.fillSize);
                            includeTimer.Start();
                            ht.Search(keysInclude[i]);
                            includeTimer.Stop();
                        }

                        for (int i = 0; i < keysExclude.Length; i++)
                        {
                            Ext.FillCache(testcase.fillSize);
                            excludeTimer.Start();
                            ht.Search(keysExclude[i]);
                            excludeTimer.Stop();
                        }
                    }

                    #region Writer
                    var rec = $"{(double)insertCount / ht.Capacity / testcase.testCount:f3}; " +
                              $"{insertProbe:d}; " +
                              $"{(double)insertProbe / insertCount:f2}; " +
                              $"{insertTimer.ElapsedTicks:d}; " +
                              $"{(double)insertTimer.ElapsedTicks / insertCount:f2}; " +
                              $"{includeProbe:d}; " +
                              $"{(double)includeProbe / includeCount:f2}; " +
                              $"{includeTimer.ElapsedTicks:d}; " +
                              $"{(double)includeTimer.ElapsedTicks / includeCount:f2}; " +
                              $"{excludeProbe:d}; " +
                              $"{(double)excludeProbe / excludeCount:f2}; " +
                              $"{excludeTimer.ElapsedTicks:d}; " +
                              $"{(double)excludeTimer.ElapsedTicks / excludeCount:f2}; " +
                              $"";
                    Console.WriteLine(rec);
                    file.WriteLine(rec);
                    file.Flush();
                    #endregion
                }

                totalTimer.Stop();
                var time = $"time; {totalTimer.Elapsed.Hours:d2}:{totalTimer.Elapsed.Minutes:d2}:{totalTimer.Elapsed.Seconds:d2}; {totalTimer.ElapsedMilliseconds}";
                Console.WriteLine(time);
                file.WriteLine(time);
                file.Flush();
            }
        }