Esempio n. 1
0
        static void TestCollections_Menu()
        {
            TestCollections testCollections = new TestCollections();
            int             user_answer;

            do
            {
                Console.WriteLine(@"1. Сгенерировать элементы коллекций
2. Определить, сколько времени требуется для поиска элементов в каждой коллекции
3. Добавить случайный элемент в конец каждой коллекции
4. Удалить случайный элемент из каждой коллекции
5. Выход");
                user_answer = InputNumber("", 1, 5);
                switch (user_answer)
                {
                case 1:
                    testCollections.Show();
                    break;

                case 2:
                    if (testCollections.size > 0)
                    {
                        testCollections.Time_Research();
                    }
                    else
                    {
                        Console.WriteLine("Коллекции пустые!");
                    }
                    break;

                case 3:
                    testCollections.Adding();
                    break;

                case 4:
                    if (testCollections.size > 0)
                    {
                        testCollections.Removing();
                    }
                    else
                    {
                        Console.WriteLine("Коллекции пустые!");
                    }
                    break;

                default: break;
                }
            } while (user_answer != 5);
        }
Esempio n. 2
0
        static void Task3()
        {
            int             length         = ReadInt(3, 1000, "=== Введите размер коллекции ===");
            TestCollections testCollection = new TestCollections(length);

            testCollection = testCollection.CreateRand();
            int       userChoice = 1;
            Stopwatch watch      = new Stopwatch();

            do
            {
                ThirdMenu();

                userChoice = ReadInt(0, 7, "=== Выберите действие ===");

                switch (userChoice)
                {
                case 1:      // Вывод

                    testCollection.Show();
                    break;

                case 2:      // Добавление объекта

                    InsuranceCompany i = new InsuranceCompany();
                    i.Input();

                    testCollection.OrganizationQueue.Enqueue(i.BaseOrganization);
                    testCollection.StringQueue.Enqueue(i.BaseOrganization.ToString());
                    testCollection.OrganizationSDictionary.Add(i.BaseOrganization, i);
                    testCollection.StringSDictionary.Add(i.BaseOrganization.ToString(), i);
                    Console.WriteLine("=== Запись добавлена ===");

                    break;

                case 3:      // Удаление объекта

                    if (testCollection.Length == 0)
                    {
                        Console.WriteLine("=== Коллекция пустая ===");
                    }
                    else
                    {
                        Organization p = new Organization();
                        p = testCollection.OrganizationQueue.Peek();
                        testCollection.OrganizationQueue.Dequeue();
                        testCollection.StringQueue.Dequeue();
                        testCollection.OrganizationSDictionary.Remove(p);
                        testCollection.StringSDictionary.Remove(p.Name);
                        Console.WriteLine("=== Первый элемент в очереди удален ===");
                    }
                    break;

                case 4:      // Сравнение скорости поиска первого элемента
                    string           key     = testCollection.CollectionStringFirst;
                    InsuranceCompany insComp = (InsuranceCompany)testCollection.CollectionFirst.Value.Clone();
                    Console.WriteLine("=== Сравнение скорости поиска первого элемента ===");

                    watch.Start();
                    bool result = testCollection.OrganizationQueue.Contains(insComp);
                    watch.Stop();
                    TimeSpan time = watch.Elapsed;
                    ShowTime("Queue<Organization>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.StringQueue.Contains(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<string>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsKey(insComp.BaseOrganization);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.StringSDictionary.ContainsKey(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<string, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsValue(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsValue", result, time);

                    break;

                case 5:      // Сравнение скорости поиска последнего элемента

                    key     = testCollection.StringQueue.Peek();
                    insComp = (InsuranceCompany)testCollection.StringSDictionary[key].Clone();

                    Console.WriteLine("=== Сравнение скорости поиска последнего элемента ===");

                    watch.Start();
                    result = testCollection.OrganizationQueue.Contains(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<Organization>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.StringQueue.Contains(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<string>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsKey(insComp.BaseOrganization);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.StringSDictionary.ContainsKey(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<string, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsValue(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsValue", result, time);

                    break;

                case 6:      // Сравнение скорости поиска центрального элемента

                    key     = testCollection.CollectionStringMiddle;
                    insComp = (InsuranceCompany)testCollection.CollectionMiddle.Value.Clone();

                    Console.WriteLine("=== Сравнение скорости поиска центрального элемента ===");

                    watch.Start();
                    result = testCollection.OrganizationQueue.Contains(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<Organization>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.StringQueue.Contains(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<string>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsKey(insComp.BaseOrganization);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.StringSDictionary.ContainsKey(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<string, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsValue(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsValue", result, time);

                    break;

                case 7:      // Сравнение скорости поиска несуществующего элемента

                    insComp = new InsuranceCompany("Не существует", "Нет города", 0, 0, "Нет региона");
                    key     = "Не существует";
                    Console.WriteLine("=== Сравнение скорости поиска несуществующего элемента ===");

                    watch.Start();
                    result = testCollection.OrganizationQueue.Contains(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<Organization>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.StringQueue.Contains(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("Queue<string>, метод Contains", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsKey(insComp.BaseOrganization);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.StringSDictionary.ContainsKey(key);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<string, InsuranceCompany>, метод ContainsKey", result, time);

                    watch.Restart();
                    result = testCollection.OrganizationSDictionary.ContainsValue(insComp);
                    watch.Stop();
                    time = watch.Elapsed;
                    ShowTime("SortedDictionary<Organization, InsuranceCompany>, метод ContainsValue", result, time);

                    break;
                }
            } while (userChoice != 0);
        }