|
4 | 4 | using System.Linq; |
5 | 5 | using System.Threading.Tasks; |
6 | 6 |
|
7 | | -namespace Open.Collections |
| 7 | +namespace Open.Collections; |
| 8 | + |
| 9 | +public class CollectionParallelBenchmark<T> : CollectionBenchmark<T> |
8 | 10 | { |
9 | | - public class CollectionParallelBenchmark<T> : CollectionBenchmark<T> |
| 11 | + public CollectionParallelBenchmark(uint size, uint repeat, Func<ICollection<T>> factory, Func<int, T> itemFactory) : base(size, repeat, factory, itemFactory) |
10 | 12 | { |
11 | | - public CollectionParallelBenchmark(uint size, uint repeat, Func<ICollection<T>> factory, Func<int, T> itemFactory) : base(size, repeat, factory, itemFactory) |
12 | | - { |
13 | | - |
14 | | - } |
15 | | - |
16 | | - protected override IEnumerable<TimedResult> TestOnceInternal() |
17 | | - { |
| 13 | + } |
18 | 14 |
|
19 | | - var c = Param(); |
| 15 | + protected override IEnumerable<TimedResult> TestOnceInternal() |
| 16 | + { |
| 17 | + var c = Param(); |
20 | 18 |
|
21 | | - yield return TimedResult.Measure("Fill (.Add(item)) (In Parallel)", () => |
22 | | - { |
23 | | - Parallel.For(0, TestSize, i => c.Add(_items[i])); |
24 | | - }); |
| 19 | + yield return TimedResult.Measure("Fill (.Add(item)) (In Parallel)", |
| 20 | + () => Parallel.For(0, TestSize, i => c.Add(_items[i]))); |
25 | 21 |
|
26 | | - yield return TimedResult.Measure("Enumerate", () => |
27 | | - { |
28 | | - // ReSharper disable once NotAccessedVariable |
29 | | - var x = 0; |
30 | | - // ReSharper disable once LoopCanBeConvertedToQuery |
31 | | - foreach (var _ in c) { x++; } |
32 | | - }); |
| 22 | + yield return TimedResult.Measure("Enumerate", () => |
| 23 | + { |
| 24 | + // ReSharper disable once NotAccessedVariable |
| 25 | + var x = 0; |
| 26 | + // ReSharper disable once LoopCanBeConvertedToQuery |
| 27 | + foreach (var _ in c) { x++; } |
| 28 | + }); |
33 | 29 |
|
34 | | - // It's obvious to note that you have to 'lock' a collection or acquire a 'snapshot' before enumerating. |
35 | | - yield return TimedResult.Measure("Enumerate (In Parallel)", () => |
36 | | - { |
37 | | - Parallel.ForEach(c, i => { }); |
38 | | - }); |
| 30 | + // It's obvious to note that you have to 'lock' a collection or acquire a 'snapshot' before enumerating. |
| 31 | + yield return TimedResult.Measure("Enumerate (In Parallel)", |
| 32 | + () => Parallel.ForEach(c, _ => { })); |
39 | 33 |
|
40 | | - yield return TimedResult.Measure(".Contains(item) (In Parallel)", () => |
41 | | - { |
42 | | - Parallel.For(0, TestSize * 2, i => { var _ = c.Contains(_items[i]); }); |
43 | | - }); |
| 34 | + yield return TimedResult.Measure(".Contains(item) (In Parallel)", |
| 35 | + () => Parallel.For(0, TestSize * 2, i => { var _ = c.Contains(_items[i]); })); |
44 | 36 |
|
45 | | - if (c is IList<T> list) |
| 37 | + if (c is IList<T> list) |
| 38 | + { |
| 39 | + yield return TimedResult.Measure("IList<T> Read Access", () => |
46 | 40 | { |
47 | | - yield return TimedResult.Measure("IList<T> Read Access", () => |
| 41 | + for (var i = 0; i < TestSize; i += 2) |
48 | 42 | { |
49 | | - for (var i = 0; i < TestSize; i += 2) |
50 | | - { |
51 | | - var _ = list[i]; |
52 | | - } |
53 | | - }); |
| 43 | + var _ = list[i]; |
| 44 | + } |
| 45 | + }); |
54 | 46 |
|
55 | | - yield return TimedResult.Measure("IList<T> Read Access (In Parallel)", () => |
56 | | - { |
57 | | - Parallel.For(0, (int)TestSize, i => { var _ = list[i]; }); |
58 | | - }); |
59 | | - } |
| 47 | + yield return TimedResult.Measure("IList<T> Read Access (In Parallel)", |
| 48 | + () => Parallel.For(0, (int)TestSize, i => { var _ = list[i]; })); |
| 49 | + } |
60 | 50 |
|
61 | | - if (c is ISynchronizedCollection<T> syncList) |
| 51 | + yield return c is ISynchronizedCollection<T> syncList |
| 52 | + ? TimedResult.Measure(".Snapshot()", () => |
62 | 53 | { |
63 | | - yield return TimedResult.Measure(".Snapshot()", () => |
64 | | - { |
65 | | - var _ = syncList.Snapshot(); |
66 | | - }); |
67 | | - } |
68 | | - else |
69 | | - { |
70 | | - yield return TimedResult.Measure(".Snapshot()", () => |
71 | | - { |
72 | | - var _ = c.ToArray(); |
73 | | - }); |
74 | | - } |
75 | | - |
76 | | - yield return TimedResult.Measure("Empty Backwards (.Remove(last)) (In Parallel)", () => |
| 54 | + var _ = syncList.Snapshot(); |
| 55 | + }) |
| 56 | + : TimedResult.Measure(".Snapshot()", () => |
77 | 57 | { |
78 | | - Parallel.For(0, TestSize, i => { c.Remove(_items[TestSize - i - 1]); }); |
| 58 | + var _ = c.ToArray(); |
79 | 59 | }); |
80 | 60 |
|
81 | | - yield return TimedResult.Measure("Refill (.Add(item)) (In Parallel)", () => |
82 | | - { |
83 | | - Parallel.For(0, TestSize, i => c.Add(_items[i])); |
84 | | - }); |
| 61 | + yield return TimedResult.Measure("Empty Backwards (.Remove(last)) (In Parallel)", |
| 62 | + () => Parallel.For(0, TestSize, i => c.Remove(_items[TestSize - i - 1]))); |
85 | 63 |
|
86 | | - yield return TimedResult.Measure("50/50 Mixed Contains/Add (In Parallel)", () => |
87 | | - { |
88 | | - Parallel.For(0, TestSize, i => |
89 | | - { |
90 | | - if (i % 2 == 0) |
91 | | - c.Contains(_items[i]); |
92 | | - else |
93 | | - c.Add(_items[i]); |
94 | | - }); |
95 | | - }); |
| 64 | + yield return TimedResult.Measure("Refill (.Add(item)) (In Parallel)", |
| 65 | + () => Parallel.For(0, TestSize, i => c.Add(_items[i]))); |
96 | 66 |
|
97 | | - yield return TimedResult.Measure("10/90 Mixed Contains/Add (In Parallel)", () => |
| 67 | + yield return TimedResult.Measure("50/50 Mixed Contains/Add (In Parallel)", |
| 68 | + () => Parallel.For(0, TestSize, i => |
98 | 69 | { |
99 | | - Parallel.For(0, TestSize, i => |
100 | | - { |
101 | | - if (i % 10 == 0) |
102 | | - c.Contains(_items[i]); |
103 | | - else |
104 | | - c.Add(_items[i]); |
105 | | - }); |
106 | | - }); |
107 | | - |
108 | | - yield return TimedResult.Measure("90/10 Mixed Contains/Add (In Parallel)", () => |
| 70 | + if (i % 2 == 0) |
| 71 | + c.Contains(_items[i]); |
| 72 | + else |
| 73 | + c.Add(_items[i]); |
| 74 | + })); |
| 75 | + |
| 76 | + yield return TimedResult.Measure("10/90 Mixed Contains/Add (In Parallel)", |
| 77 | + () => Parallel.For(0, TestSize, i => |
109 | 78 | { |
110 | | - Parallel.For(0, TestSize, i => |
111 | | - { |
112 | | - if (i % 10 != 9) |
113 | | - c.Contains(_items[i]); |
114 | | - else |
115 | | - c.Add(_items[i]); |
116 | | - }); |
117 | | - }); |
118 | | - |
119 | | - yield return TimedResult.Measure("50/50 Mixed Add/Remove (In Parallel)", () => |
| 79 | + if (i % 10 == 0) |
| 80 | + c.Contains(_items[i]); |
| 81 | + else |
| 82 | + c.Add(_items[i]); |
| 83 | + })); |
| 84 | + |
| 85 | + yield return TimedResult.Measure("90/10 Mixed Contains/Add (In Parallel)", |
| 86 | + () => Parallel.For(0, TestSize, i => |
120 | 87 | { |
121 | | - Parallel.For(0, TestSize, i => |
122 | | - { |
123 | | - if (i % 2 == 0) |
124 | | - c.Add(_items[i]); |
125 | | - else |
126 | | - c.Remove(_items[i]); |
127 | | - }); |
128 | | - }); |
129 | | - |
130 | | - yield return TimedResult.Measure("Empty Forwards (.Remove(first)) (In Parallel)", () => |
| 88 | + if (i % 10 != 9) |
| 89 | + c.Contains(_items[i]); |
| 90 | + else |
| 91 | + c.Add(_items[i]); |
| 92 | + })); |
| 93 | + |
| 94 | + yield return TimedResult.Measure("50/50 Mixed Add/Remove (In Parallel)", |
| 95 | + () => Parallel.For(0, TestSize, i => |
131 | 96 | { |
132 | | - Parallel.For(0, TestSize, i => { c.Remove(_items[i]); }); |
133 | | - }); |
134 | | - |
135 | | - } |
136 | | - |
137 | | - |
138 | | - |
| 97 | + if (i % 2 == 0) |
| 98 | + c.Add(_items[i]); |
| 99 | + else |
| 100 | + c.Remove(_items[i]); |
| 101 | + })); |
| 102 | + |
| 103 | + yield return TimedResult.Measure("Empty Forwards (.Remove(first)) (In Parallel)", |
| 104 | + () => Parallel.For(0, TestSize, i => c.Remove(_items[i]))); |
139 | 105 | } |
| 106 | +} |
140 | 107 |
|
141 | | - public class CollectionParallelBenchmark : CollectionParallelBenchmark<object> |
| 108 | +public class CollectionParallelBenchmark : CollectionParallelBenchmark<object> |
| 109 | +{ |
| 110 | + public CollectionParallelBenchmark(uint size, uint repeat, Func<ICollection<object>> factory) |
| 111 | + : base(size, repeat, factory, _ => new object()) |
142 | 112 | { |
143 | | - public CollectionParallelBenchmark(uint size, uint repeat, Func<ICollection<object>> factory) : base(size, repeat, factory, i => new object()) |
144 | | - { |
145 | | - |
146 | | - } |
147 | | - |
148 | | - public static TimedResult[] Results<T>(uint size, uint repeat, Func<ICollection<T>> factory, Func<int, T> itemFactory) => (new CollectionParallelBenchmark<T>(size, repeat, factory, itemFactory)).Result; |
149 | | - |
150 | | - |
151 | | - public static TimedResult[] Results<T>(uint size, uint repeat, Func<ICollection<T>> factory) |
152 | | - where T : new() => (new CollectionParallelBenchmark<T>(size, repeat, factory, i => new T())).Result; |
153 | | - |
154 | 113 | } |
155 | 114 |
|
| 115 | + public static TimedResult[] Results<T>(uint size, uint repeat, Func<ICollection<T>> factory, Func<int, T> itemFactory) |
| 116 | + => new CollectionParallelBenchmark<T>(size, repeat, factory, itemFactory).Result; |
| 117 | + |
| 118 | + public static TimedResult[] Results<T>(uint size, uint repeat, Func<ICollection<T>> factory) |
| 119 | + where T : new() |
| 120 | + => new CollectionParallelBenchmark<T>(size, repeat, factory, _ => new T()).Result; |
156 | 121 | } |
0 commit comments