| | 1 | | using BenchmarkDotNet.Attributes; |
| | 2 | | using PointCloud.Benchmark.Util; |
| | 3 | | using ReFlex.Core.Common.Components; |
| | 4 | |
|
| | 5 | | namespace PointCloud.Benchmark.Benchmarks.IteratePointCloud; |
| | 6 | |
|
| | 7 | | [MemoryDiagnoser] |
| | 8 | | public class IterationMethods2 |
| | 9 | | { |
| | 10 | | private int _width; |
| | 11 | | private int _height; |
| | 12 | |
|
| | 13 | | private Common.PointCloud3 _pCloud3; |
| | 14 | |
|
| 8 | 15 | | private int _stopRow = 150; |
| 8 | 16 | | private int _stopCol = 200; |
| | 17 | |
|
| 4 | 18 | | public Common.PointCloud3 Points { get => _pCloud3; } |
| | 19 | |
|
| | 20 | | public void ComputeColRow(int idx, out int x, out int y) |
| 2147752 | 21 | | { |
| 2147752 | 22 | | x = idx % _width; |
| 2147752 | 23 | | y = idx / _width; |
| 2147752 | 24 | | } |
| | 25 | |
|
| | 26 | | public void ComputeIndex(int x, int y, out int index) |
| 2173052 | 27 | | { |
| 2173052 | 28 | | index = y * _width + x; |
| 2173052 | 29 | | } |
| | 30 | |
|
| 4 | 31 | | public IterationMethods2() |
| 4 | 32 | | { |
| 4 | 33 | | var data = DataLoader.Load(); |
| 4 | 34 | | Init(data.Item3, data.Item1, data.Item2); |
| | 35 | |
|
| 4 | 36 | | } |
| | 37 | |
|
| 4 | 38 | | public IterationMethods2(Point3[] data, int width, int height) |
| 4 | 39 | | { |
| 4 | 40 | | Init(data, width, height); |
| 4 | 41 | | } |
| | 42 | |
|
| | 43 | | private void Init(Point3[] data, int width, int height) |
| 8 | 44 | | { |
| 8 | 45 | | _width = width; |
| 8 | 46 | | _height = height; |
| | 47 | |
|
| 8 | 48 | | _stopRow = (int)(0.66 * _height); |
| 8 | 49 | | _stopCol = (int)(0.75 * _width); |
| | 50 | |
|
| 8 | 51 | | _pCloud3 = new Common.PointCloud3(_width, _height); |
| 8 | 52 | | _pCloud3.Update(data); |
| 8 | 53 | | } |
| | 54 | |
|
| | 55 | |
|
| | 56 | | [Benchmark] |
| | 57 | | public Tuple<float, int> Iterate2dIndexReconstruction() |
| 1 | 58 | | { |
| 1 | 59 | | var value = 0f; |
| 1 | 60 | | var n = 0; |
| 222 | 61 | | for (var x = 0; x < _pCloud3.SizeX; x++) |
| 110 | 62 | | { |
| 50820 | 63 | | for (var y = 0; y < _pCloud3.SizeY; y++) |
| 25300 | 64 | | { |
| 25300 | 65 | | ComputeIndex(x, y, out var i); |
| 37682 | 66 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 67 | | value += _pCloud3[i].Z; |
| 12382 | 68 | | n++; |
| 12382 | 69 | | } |
| 25300 | 70 | | } |
| 110 | 71 | | } |
| 1 | 72 | | return new Tuple<float, int>(value, n); |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | [Benchmark] |
| | 76 | | public Tuple<float, int> Iterate2dIndex() |
| 1 | 77 | | { |
| 1 | 78 | | var value = 0f; |
| 1 | 79 | | var n = 0; |
| 222 | 80 | | for (var x = 0; x < _pCloud3.SizeX; x++) |
| 110 | 81 | | { |
| 50820 | 82 | | for (var y = 0; y < _pCloud3.SizeY; y++) |
| 25300 | 83 | | { |
| 37682 | 84 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 85 | | value += _pCloud3[x, y].Z; |
| 12382 | 86 | | n++; |
| 12382 | 87 | | } |
| 25300 | 88 | | } |
| | 89 | |
|
| 110 | 90 | | } |
| 1 | 91 | | return new Tuple<float, int>(value, n); |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | [Benchmark] |
| | 95 | | public Tuple<float, int> IterateArray() |
| 1 | 96 | | { |
| 1 | 97 | | var value = 0f; |
| 1 | 98 | | var n = 0; |
| 1 | 99 | | var array = _pCloud3.AsArray(); |
| | 100 | |
|
| 222 | 101 | | for (var x = 0; x < _pCloud3.SizeX; x++) |
| 110 | 102 | | { |
| 50820 | 103 | | for (var y = 0; y < _pCloud3.SizeY; y++) |
| 25300 | 104 | | { |
| 25300 | 105 | | ComputeIndex(x, y, out var i); |
| 37682 | 106 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 107 | | value += array[i].Z; |
| 12382 | 108 | | n++; |
| 12382 | 109 | | } |
| 25300 | 110 | | } |
| 110 | 111 | | } |
| 1 | 112 | | return new Tuple<float, int>(value, n); |
| 1 | 113 | | } |
| | 114 | |
|
| | 115 | | [Benchmark] |
| | 116 | | public Tuple<float, int> IterateSpan() |
| 1 | 117 | | { |
| 1 | 118 | | var value = 0f; |
| 1 | 119 | | var n = 0; |
| 1 | 120 | | var span = _pCloud3.AsArray(); |
| 222 | 121 | | for (var x = 0; x < _pCloud3.SizeX; x++) |
| 110 | 122 | | { |
| 50820 | 123 | | for (var y = 0; y < _pCloud3.SizeY; y++) |
| 25300 | 124 | | { |
| 25300 | 125 | | ComputeIndex(x, y, out var i); |
| 37682 | 126 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 127 | | value += span[i].Z; |
| 12382 | 128 | | n++; |
| 12382 | 129 | | } |
| 25300 | 130 | | } |
| 110 | 131 | | } |
| 1 | 132 | | return new Tuple<float, int>(value, n); |
| 1 | 133 | | } |
| | 134 | |
|
| | 135 | | [Benchmark] |
| | 136 | | public Tuple<float, int> IterateArrayCommutative() |
| 1 | 137 | | { |
| 1 | 138 | | var value = 0f; |
| 1 | 139 | | var n = 0; |
| 1 | 140 | | var array = _pCloud3.AsArray(); |
| | 141 | |
|
| 75902 | 142 | | for (var i = 0; i < _pCloud3.Size; i++) { |
| 25300 | 143 | | ComputeColRow(i, out var x, out var y); |
| 37682 | 144 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 145 | | value += array[i].Z; |
| 12382 | 146 | | n++; |
| 12382 | 147 | | } |
| 25300 | 148 | | } |
| 1 | 149 | | return new Tuple<float, int>(value, n); |
| 1 | 150 | | } |
| | 151 | |
|
| | 152 | | [Benchmark] |
| | 153 | | public Tuple<float, int> IterateJaggedArray() |
| 1 | 154 | | { |
| 1 | 155 | | var value = 0f; |
| 1 | 156 | | var n = 0; |
| 1 | 157 | | var array = _pCloud3.AsJaggedArray(); |
| 222 | 158 | | for (var x = 0; x < _pCloud3.SizeX; x++) |
| 110 | 159 | | { |
| 50820 | 160 | | for (var y = 0; y < _pCloud3.SizeY; y++) |
| 25300 | 161 | | { |
| 37682 | 162 | | if (x < _stopCol && y < _stopRow) { |
| 12382 | 163 | | value += array[x][y].Z; |
| 12382 | 164 | | n++; |
| 12382 | 165 | | } |
| 25300 | 166 | | } |
| | 167 | |
|
| 110 | 168 | | } |
| 1 | 169 | | return new Tuple<float, int>(value, n); |
| 1 | 170 | | } |
| | 171 | |
|
| | 172 | | [Benchmark] |
| | 173 | | public Tuple<float, int> IterateSpanCommutative() |
| 1 | 174 | | { |
| 1 | 175 | | var value = 0f; |
| 1 | 176 | | var n = 0; |
| 1 | 177 | | var span = _pCloud3.AsSpan(); |
| 50602 | 178 | | for (var i = 0; i < _pCloud3.Size; i++) |
| 25300 | 179 | | { |
| 25300 | 180 | | ComputeColRow(i, out var x, out var y); |
| 25300 | 181 | | if (x < _stopCol && y < _stopRow) |
| 12382 | 182 | | { |
| 12382 | 183 | | value += span[i].Z; |
| 12382 | 184 | | n++; |
| 12382 | 185 | | } |
| 25300 | 186 | | } |
| 1 | 187 | | return new Tuple<float, int>(value, n); |
| 1 | 188 | | } |
| | 189 | | } |