|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using BenchmarkDotNet.Configs; |
| 6 | +using BenchmarkDotNet.Jobs; |
| 7 | +using Kibnet; // Assuming IntSet is in this namespace |
| 8 | + |
| 9 | +namespace IntSet.Benchmarks |
| 10 | +{ |
| 11 | + [MemoryDiagnoser] |
| 12 | + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 13 | + [CategoriesColumn] |
| 14 | + // Optional: Add [SimpleJob(RuntimeMoniker.Net80)] or other job configurations if needed |
| 15 | + public class SetOperationsBenchmarks |
| 16 | + { |
| 17 | + // Parameters for benchmark variations |
| 18 | + [Params(0, 100, 10000, 1000000)] // Added 0 to test edge cases |
| 19 | + public int Size; |
| 20 | + |
| 21 | + [Params("Dense", "Sparse")] |
| 22 | + public string DataType; |
| 23 | + |
| 24 | + private List<int> _dataA; |
| 25 | + private List<int> _dataB; // For binary set operations |
| 26 | + |
| 27 | + private Kibnet.IntSet _intSetA; // Fully qualify to avoid ambiguity if any |
| 28 | + private HashSet<int> _hashSetA; |
| 29 | + |
| 30 | + private Kibnet.IntSet _intSetB; // For binary set operations |
| 31 | + private HashSet<int> _hashSetB; // For binary set operations |
| 32 | + |
| 33 | + private int _itemToAdd; |
| 34 | + private int _itemToRemove; |
| 35 | + private int _itemToContain; |
| 36 | + |
| 37 | + [GlobalSetup] |
| 38 | + public void GlobalSetup() |
| 39 | + { |
| 40 | + Random rand = new Random(42); // Use a fixed seed for reproducibility |
| 41 | + if (DataType == "Dense") |
| 42 | + { |
| 43 | + _dataA = Enumerable.Range(0, Size).ToList(); |
| 44 | + // Ensure _dataB is also scaled by Size for dense, and has some overlap and some difference |
| 45 | + _dataB = Enumerable.Range(Size / 2, Size).ToList(); |
| 46 | + } |
| 47 | + else // Sparse |
| 48 | + { |
| 49 | + // Generate Size unique random numbers for sparse data for _dataA |
| 50 | + var sparseA = new HashSet<int>(); |
| 51 | + while(sparseA.Count < Size) |
| 52 | + { |
| 53 | + sparseA.Add(rand.Next(0, Size * 10)); |
| 54 | + } |
| 55 | + _dataA = sparseA.ToList(); |
| 56 | + _dataA.Sort(); // Optional: Sort if order matters for setup, though not for sets |
| 57 | + |
| 58 | + // Generate Size unique random numbers for sparse data for _dataB |
| 59 | + var sparseB = new HashSet<int>(); |
| 60 | + while(sparseB.Count < Size) |
| 61 | + { |
| 62 | + // Ensure some potential overlap and difference with _dataA |
| 63 | + sparseB.Add(rand.Next(0, Size * 10) + (Size * 5)); |
| 64 | + } |
| 65 | + _dataB = sparseB.ToList(); |
| 66 | + _dataB.Sort(); // Optional |
| 67 | + } |
| 68 | + |
| 69 | + // Determine items for Add, Remove, Contains operations |
| 70 | + if (Size > 0) |
| 71 | + { |
| 72 | + // Item not in _dataA for Add |
| 73 | + _itemToAdd = DataType == "Dense" ? Size : _dataA.Max() + 1; |
| 74 | + if (_dataA.Contains(_itemToAdd)) // Ensure it's truly not in for sparse random |
| 75 | + { |
| 76 | + _itemToAdd = _dataA.Max() + rand.Next(1,100); |
| 77 | + while(_dataA.Contains(_itemToAdd)) { // find one not in the set |
| 78 | + _itemToAdd++; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + _itemToRemove = _dataA[Size / 2]; // Item in _dataA |
| 83 | + _itemToContain = _dataA[Size / 2]; // Item in _dataA |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + _itemToAdd = 0; // Item to add to an empty set |
| 88 | + _itemToRemove = 0; // Item to attempt to remove from an empty set |
| 89 | + _itemToContain = 0; // Item to check in an empty set |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [IterationSetup] |
| 94 | + public void IterationSetup() |
| 95 | + { |
| 96 | + // Initialize sets for each iteration to ensure a clean state |
| 97 | + // Pass empty list if _dataA or _dataB is null (e.g. if Size is 0 and GlobalSetup didn't init them) |
| 98 | + _intSetA = new Kibnet.IntSet(_dataA ?? new List<int>()); |
| 99 | + _hashSetA = new HashSet<int>(_dataA ?? new List<int>()); |
| 100 | + |
| 101 | + _intSetB = new Kibnet.IntSet(_dataB ?? new List<int>()); |
| 102 | + _hashSetB = new HashSet<int>(_dataB ?? new List<int>()); |
| 103 | + } |
| 104 | + |
| 105 | + // --- Add Operation --- |
| 106 | + [BenchmarkCategory("Add"), Benchmark] |
| 107 | + public void IntSet_Add() => _intSetA.Add(_itemToAdd); |
| 108 | + |
| 109 | + [BenchmarkCategory("Add"), Benchmark] |
| 110 | + public void HashSet_Add() => _hashSetA.Add(_itemToAdd); |
| 111 | + |
| 112 | + // --- Contains Operation --- |
| 113 | + [BenchmarkCategory("Contains"), Benchmark] |
| 114 | + public bool IntSet_Contains() => _intSetA.Contains(_itemToContain); |
| 115 | + |
| 116 | + [BenchmarkCategory("Contains"), Benchmark] |
| 117 | + public bool HashSet_Contains() => _hashSetA.Contains(_itemToContain); |
| 118 | + |
| 119 | + // --- Remove Operation --- |
| 120 | + [BenchmarkCategory("Remove"), Benchmark] |
| 121 | + public bool IntSet_Remove() // Return bool for consistency with HashSet.Remove |
| 122 | + { |
| 123 | + if (Size > 0) return _intSetA.Remove(_itemToRemove); |
| 124 | + return false; // Or handle as appropriate for empty set |
| 125 | + } |
| 126 | + |
| 127 | + [BenchmarkCategory("Remove"), Benchmark] |
| 128 | + public bool HashSet_Remove() |
| 129 | + { |
| 130 | + if (Size > 0) return _hashSetA.Remove(_itemToRemove); |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + // --- UnionWith Operation --- |
| 135 | + [BenchmarkCategory("Union"), Benchmark] |
| 136 | + public void IntSet_UnionWith() => _intSetA.UnionWith(_intSetB); |
| 137 | + |
| 138 | + [BenchmarkCategory("Union"), Benchmark] |
| 139 | + public void HashSet_UnionWith() => _hashSetA.UnionWith(_hashSetB); |
| 140 | + |
| 141 | + // --- IntersectWith Operation --- |
| 142 | + [BenchmarkCategory("Intersection"), Benchmark] |
| 143 | + public void IntSet_IntersectWith() => _intSetA.IntersectWith(_intSetB); |
| 144 | + |
| 145 | + [BenchmarkCategory("Intersection"), Benchmark] |
| 146 | + public void HashSet_IntersectWith() => _hashSetA.IntersectWith(_hashSetB); |
| 147 | + |
| 148 | + // --- ExceptWith Operation --- |
| 149 | + [BenchmarkCategory("Except"), Benchmark] |
| 150 | + public void IntSet_ExceptWith() => _intSetA.ExceptWith(_intSetB); |
| 151 | + |
| 152 | + [BenchmarkCategory("Except"), Benchmark] |
| 153 | + public void HashSet_ExceptWith() => _hashSetA.ExceptWith(_hashSetB); |
| 154 | + |
| 155 | + // --- SymmetricExceptWith Operation --- |
| 156 | + [BenchmarkCategory("SymmetricExcept"), Benchmark] |
| 157 | + public void IntSet_SymmetricExceptWith() => _intSetA.SymmetricExceptWith(_intSetB); |
| 158 | + |
| 159 | + [BenchmarkCategory("SymmetricExcept"), Benchmark] |
| 160 | + public void HashSet_SymmetricExceptWith() => _hashSetA.SymmetricExceptWith(_hashSetB); |
| 161 | + } |
| 162 | +} |
0 commit comments