|
1 | 1 | using BenchmarkDotNet.Attributes; |
2 | 2 | using BenchmarkDotNet.Configs; |
3 | 3 |
|
4 | | -namespace HashSetvsSortedSetInCSharp |
| 4 | +namespace HashSetvsSortedSetInCSharp; |
| 5 | + |
| 6 | +[MemoryDiagnoser] |
| 7 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)] |
| 8 | +[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)] |
| 9 | +[RankColumn] |
| 10 | +public class Operations |
5 | 11 | { |
6 | | - [MemoryDiagnoser] |
7 | | - [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)] |
8 | | - [Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)] |
9 | | - [RankColumn] |
10 | | - public class Operations |
| 12 | + private readonly List<int> _numList; |
| 13 | + private readonly HashSet<int> _hashSet; |
| 14 | + private readonly SortedSet<int> _sortedSet; |
| 15 | + private readonly int _searchValue; |
| 16 | + private HashSet<int> _hashSetCopy = []; |
| 17 | + private SortedSet<int> _sortedSetCopy = []; |
| 18 | + |
| 19 | + public Operations() |
| 20 | + { |
| 21 | + _numList = RandomInts(1000000); |
| 22 | + _hashSet = InitializeIntHashSet(); |
| 23 | + _sortedSet = InitializeIntSortedSet(); |
| 24 | + _searchValue = int.MaxValue - 1; |
| 25 | + } |
| 26 | + |
| 27 | + [Benchmark] |
| 28 | + public HashSet<int> InitializeIntHashSet() |
11 | 29 | { |
12 | | - private readonly List<int> _numList; |
13 | | - private readonly HashSet<int> _hashSet; |
14 | | - private readonly SortedSet<int> _sortedSet; |
15 | | - private readonly int _searchValue; |
| 30 | + var hashSet = new HashSet<int>(); |
16 | 31 |
|
17 | | - public Operations() |
| 32 | + foreach (var number in _numList) |
18 | 33 | { |
19 | | - _numList = RandomInts(1000000); |
20 | | - _hashSet = InitializeIntHashSet(); |
21 | | - _sortedSet = InitializeIntSortedSet(); |
22 | | - _searchValue = Int32.MaxValue - 1; |
| 34 | + hashSet.Add(number); |
23 | 35 | } |
24 | 36 |
|
25 | | - [Benchmark] |
26 | | - public HashSet<int> InitializeIntHashSet() |
27 | | - { |
28 | | - var hashSet = new HashSet<int>(); |
| 37 | + return hashSet; |
| 38 | + } |
29 | 39 |
|
30 | | - foreach (var number in _numList) |
31 | | - { |
32 | | - hashSet.Add(number); |
33 | | - } |
| 40 | + [Benchmark] |
| 41 | + public SortedSet<int> InitializeIntSortedSet() |
| 42 | + { |
| 43 | + var sortedSet = new SortedSet<int>(); |
34 | 44 |
|
35 | | - return hashSet; |
| 45 | + foreach (var number in _numList) |
| 46 | + { |
| 47 | + sortedSet.Add(number); |
36 | 48 | } |
37 | 49 |
|
38 | | - [Benchmark] |
39 | | - public SortedSet<int> InitializeIntSortedSet() |
40 | | - { |
41 | | - var sortedSet = new SortedSet<int>(); |
| 50 | + return sortedSet; |
| 51 | + } |
42 | 52 |
|
43 | | - foreach (var number in _numList) |
44 | | - { |
45 | | - sortedSet.Add(number); |
46 | | - } |
| 53 | + [Benchmark] |
| 54 | + public HashSet<int> RemoveElementFromHashSet() |
| 55 | + { |
| 56 | + _hashSet.Remove(_searchValue); |
47 | 57 |
|
48 | | - return sortedSet; |
49 | | - } |
| 58 | + return _hashSet; |
| 59 | + } |
50 | 60 |
|
51 | | - [Benchmark] |
52 | | - public HashSet<int> RemoveElementFromHashSet() |
53 | | - { |
54 | | - _hashSet.Remove(_searchValue); |
| 61 | + [Benchmark] |
| 62 | + public SortedSet<int> RemoveElementFromSortedSet() |
| 63 | + { |
| 64 | + _sortedSet.Remove(_searchValue); |
55 | 65 |
|
56 | | - return _hashSet; |
57 | | - } |
| 66 | + return _sortedSet; |
| 67 | + } |
58 | 68 |
|
59 | | - [Benchmark] |
60 | | - public HashSet<int> RemoveWhereFromHashSet() |
61 | | - { |
62 | | - _hashSet.RemoveWhere(IsOdd); |
| 69 | + [IterationSetup(Targets = [nameof(RemoveWhereFromHashSet), nameof(RemoveWhereFromSortedSet)])] |
| 70 | + public void CopySets() |
| 71 | + { |
| 72 | + _hashSetCopy = new HashSet<int>(_hashSet); |
| 73 | + _sortedSetCopy = new SortedSet<int>(_sortedSet); |
| 74 | + } |
63 | 75 |
|
64 | | - return _hashSet; |
65 | | - } |
| 76 | + [Benchmark] |
| 77 | + public HashSet<int> RemoveWhereFromHashSet() |
| 78 | + { |
| 79 | + _hashSetCopy.RemoveWhere(IsOdd); |
66 | 80 |
|
67 | | - [Benchmark] |
68 | | - public SortedSet<int> RemoveElementFromSortedSet() |
69 | | - { |
70 | | - _sortedSet.Remove(_searchValue); |
| 81 | + return _hashSetCopy; |
| 82 | + } |
71 | 83 |
|
72 | | - return _sortedSet; |
73 | | - } |
| 84 | + [Benchmark] |
| 85 | + public SortedSet<int> RemoveWhereFromSortedSet() |
| 86 | + { |
| 87 | + _sortedSetCopy.RemoveWhere(IsOdd); |
74 | 88 |
|
75 | | - [Benchmark] |
76 | | - public SortedSet<int> RemoveWhereFromSortedSet() |
77 | | - { |
78 | | - _sortedSet.RemoveWhere(IsOdd); |
| 89 | + return _sortedSetCopy; |
| 90 | + } |
79 | 91 |
|
80 | | - return _sortedSet; |
81 | | - } |
| 92 | + [Benchmark] |
| 93 | + public bool SearchSortedSet() |
| 94 | + { |
| 95 | + return _sortedSet.Contains(_searchValue); |
| 96 | + } |
82 | 97 |
|
83 | | - [Benchmark] |
84 | | - public bool SearchSortedSet() |
85 | | - { |
86 | | - return _sortedSet.Contains(_searchValue); |
87 | | - } |
| 98 | + [Benchmark] |
| 99 | + public bool SearchHashSet() |
| 100 | + { |
| 101 | + return _hashSet.Contains(_searchValue); |
| 102 | + } |
88 | 103 |
|
89 | | - [Benchmark] |
90 | | - public bool SearchHashSet() |
91 | | - { |
92 | | - return _hashSet.Contains(_searchValue); |
93 | | - } |
| 104 | + [Benchmark] |
| 105 | + public List<int> SortHashSetElements() |
| 106 | + { |
| 107 | + return _hashSet.Order().ToList(); |
| 108 | + } |
94 | 109 |
|
95 | | - [Benchmark] |
96 | | - public List<int> SortHashSetElements() |
97 | | - { |
98 | | - var sortElements = _hashSet.OrderBy(element => element).ToList(); |
99 | | - |
100 | | - return sortElements; |
101 | | - } |
| 110 | + [Benchmark] |
| 111 | + public List<int> SortSortedSetElements() |
| 112 | + { |
| 113 | + return _sortedSet.ToList(); |
| 114 | + } |
102 | 115 |
|
103 | | - [Benchmark] |
104 | | - public List<int> SortSortedSetElements() |
105 | | - { |
106 | | - return _sortedSet.ToList(); |
107 | | - } |
| 116 | + private static List<int> RandomInts(int size) |
| 117 | + { |
| 118 | + var rand = new Random(); |
| 119 | + List<int> numbers = []; |
108 | 120 |
|
109 | | - private List<int> RandomInts(int size) |
| 121 | + for (int i = 0; i < size - 1; i++) |
110 | 122 | { |
111 | | - var rand = new Random(); |
112 | | - var numbers = new List<int>(); |
113 | | - |
114 | | - for (int i = 0; i < size - 1; i++) |
115 | | - { |
116 | | - numbers.Add(rand.Next()); |
117 | | - } |
| 123 | + numbers.Add(rand.Next()); |
| 124 | + } |
118 | 125 |
|
119 | | - numbers.Add(Int32.MaxValue - 1); |
| 126 | + numbers.Add(int.MaxValue - 1); |
120 | 127 |
|
121 | | - return numbers; |
122 | | - } |
| 128 | + return numbers; |
| 129 | + } |
123 | 130 |
|
124 | | - private bool IsOdd(int num) |
125 | | - { |
126 | | - return num % 2 == 1; |
127 | | - } |
| 131 | + private static bool IsOdd(int num) |
| 132 | + { |
| 133 | + return num % 2 == 1; |
128 | 134 | } |
129 | 135 | } |
0 commit comments