public void AddTest() { ConcurrentObservableSortedCollection <int> subject = new ConcurrentObservableSortedCollection <int>(); using (var benchmark = new BenchmarkIt("Adding items to sorted collection")) { // Create test subject // Populate test subject _testCollections.AsParallel().ForAll(collection => { foreach (var item in collection) { subject.Add(item); } }); } // Compare test subject with expected result Assert.AreEqual(subject.Count, _sortedCollection.Count); bool itemsEqual = _sortedCollection .Zip(subject, (a, b) => a == b) .All(b => b); Assert.IsTrue(itemsEqual); // Compare collectionView var view = subject.CollectionView; Assert.AreEqual(view.Count, _sortedCollection.Count); bool viewItemsEqual = _sortedCollection .Zip(view, (a, b) => a == b) .All(b => b); Assert.IsTrue(viewItemsEqual); }
private void GenerateTestCollections() { using (var benchmark = new BenchmarkIt("Generating Test Input Collections for sorted collection")) { // Use a fixed seed for consistency in results Random random = new Random(1); // Create 10 test sets for (int collection = 0; collection < _testCollectionCount; ++collection) { List <int> testCollection = new List <int>(); for (int item = 0; item < _itemsPerCollection; ++item) { // Ensure we have some duplicates by picking a random number // less than half the number of items. testCollection.Add(random.Next(_itemsPerCollection / 2)); } _testCollections.Add(testCollection); } } using (var benchmark = new BenchmarkIt("Generating Expected Output for sorted collection")) { _sortedCollection = _testCollections .SelectMany(x => x) .OrderBy(x => x) .ToList(); } }
private void GenerateSortedSet() { using (var benchmark = new BenchmarkIt("Generating Expected Output for sorted set")) { _sortedSet = _testCollections .SelectMany(x => x) .Distinct() .OrderBy(x => x) .ToList(); } }
private void TimeImmutableSortedSet() { var sortedSet = ImmutableSortedSet <int> .Empty; using (var benchmark = new BenchmarkIt("Timing adding everything to an immutable sorted set")) { foreach (var collection in _testCollections) { foreach (var item in collection) { sortedSet = sortedSet.Add(item); } } } }
private void TimeNormalSortedSet() { var sortedSet = new SortedSet <int>(); using (var benchmark = new BenchmarkIt("Timing adding everything to normal sorted set")) { foreach (var collection in _testCollections) { foreach (var item in collection) { sortedSet.Add(item); } } } }
public void AddTest() { int _testCollectionCount = 10; int _itemsPerCollection = 200_000; List <List <int> > _testCollections = new List <List <int> >(); List <int> _sortedCollection = new List <int>(); // Use a fixed seed for consistency in results Random random = new Random(1); // Create 10 test sets for (int collection = 0; collection < _testCollectionCount; ++collection) { List <int> testCollection = new List <int>(); for (int item = 0; item < _itemsPerCollection; ++item) { // Ensure we have some duplicates by picking a random number // less than half the number of items. testCollection.Add(random.Next(_itemsPerCollection / 2)); } _testCollections.Add(testCollection); } _sortedCollection = _testCollections .SelectMany(x => x) .Distinct() .OrderBy(x => x) .ToList(); ConcurrentObservableSortedDictionary <int, int> subject = new ConcurrentObservableSortedDictionary <int, int>(); using (var benchmark = new BenchmarkIt("Adding items to sorted dictionary")) { // Create test subject // Populate test subject _testCollections.AsParallel().ForAll(collection => { foreach (var item in collection) { subject[item] = item; } }); } bool keyMatchesValue = subject .All(kv => kv.Key == kv.Value); Assert.IsTrue(keyMatchesValue); bool isSorted = subject .Aggregate( new { Sorted = true, LastKey = (int?)null }, (a, b) => new { Sorted = a.Sorted && (!a.LastKey.HasValue || a.LastKey.Value < b.Key), LastKey = (int?)b.Key }) .Sorted; Assert.IsTrue(isSorted); // Compare test subject with expected result Assert.AreEqual(subject.Count, _sortedCollection.Count); bool itemsEqual = _sortedCollection .Zip(subject, (a, b) => a == b.Value) .All(b => b); Assert.IsTrue(itemsEqual); // Compare collectionView var view = subject.CollectionView; Assert.AreEqual(view.Count, _sortedCollection.Count); bool viewItemsEqual = _sortedCollection .Zip(view, (a, b) => a == b.Value) .All(b => b); Assert.IsTrue(viewItemsEqual); }