private void FixedTimeOfAddAndContainsToDictionary(int countOfElements) { Dictionary <Person, string> referenceBookOfPersons = GeneratePersonsDictionary(countOfElements); Console.WriteLine("Время, затраченное на добавление " + countOfElements + " элементов c генерированным хешем: " + stopwatch.ElapsedMilliseconds + " миллисекунд"); Console.WriteLine(); Dictionary <PersonWithHashCodeConst, string> referenceBookOfPersonsWithHashCodeConst = GeneratePersonsWithHashCodeConstDictionary(countOfElements); Console.WriteLine("Время, затраченное на добавление " + countOfElements + " элементов с хешем-const: " + stopwatch.ElapsedMilliseconds + " миллисекунд"); Dictionary <Person, string> .KeyCollection keys = referenceBookOfPersons.Keys; Person[] keysMass = new Person[keys.Count]; keys.CopyTo(keysMass, 0); int randomIndex1 = _rand.Next(0, keys.Count); Person person1 = keysMass[randomIndex1]; IsContainsInDictionary(referenceBookOfPersons, person1); Console.WriteLine("Время, затраченное на поиск элемента в словаре с " + countOfElements + " элементов с генерированным хешем: " + stopwatch.ElapsedMilliseconds + " миллисекунд"); Dictionary <PersonWithHashCodeConst, string> .KeyCollection keysOfBadCollection = referenceBookOfPersonsWithHashCodeConst.Keys; PersonWithHashCodeConst[] keysOfBadCollectionMass = new PersonWithHashCodeConst[keysOfBadCollection.Count]; keysOfBadCollection.CopyTo(keysOfBadCollectionMass, 0); int randomIndex2 = _rand.Next(0, keysOfBadCollection.Count); PersonWithHashCodeConst person2 = keysOfBadCollectionMass[randomIndex2]; IsContainsInBadDictionary(referenceBookOfPersonsWithHashCodeConst, person2); Console.WriteLine("Время, затраченное на поиск элемента в словаре с " + countOfElements + " элементов с хешем-const: " + stopwatch.ElapsedMilliseconds + " миллисекунд"); }
private Dictionary <PersonWithHashCodeConst, string> GeneratePersonsWithHashCodeConstDictionary(int count) { if (count <= 0) { throw new ArgumentException("Недопустимое значение для количества элементов в справочнике"); } stopwatch.Reset(); Dictionary <PersonWithHashCodeConst, string> dictionary = new Dictionary <PersonWithHashCodeConst, string>(); while (count > 0) { try { PersonWithHashCodeConst person = GeneratorOfPersons.GeneratePersonWithHashCodeConst(); string plaseOfWork = GeneratorOfPersons.GeneratePlaceOfWork(); stopwatch.Start(); dictionary.Add(person, plaseOfWork); stopwatch.Stop(); count--; } catch (ArgumentException) { Console.WriteLine("Ошибка про добавлении элемента в справочник."); } } return(dictionary); }
private bool IsContainsInBadDictionary(Dictionary <PersonWithHashCodeConst, string> dictionary, PersonWithHashCodeConst person) { stopwatch.Restart(); bool result = dictionary.ContainsKey(person); stopwatch.Stop(); return(result); }