|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Globalization; |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Sdk; |
| 12 | +using Microsoft.Xunit.Performance; |
| 13 | + |
| 14 | +namespace System.Numerics.Tests |
| 15 | +{ |
| 16 | + public static class Constructor |
| 17 | + { |
| 18 | + private static Random s_random = new Random(); |
| 19 | + |
| 20 | + public const int DefaultInnerIterationsCount = 100000000; |
| 21 | + |
| 22 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 23 | + public static void ConstructorBenchmark_Byte() |
| 24 | + { |
| 25 | + Byte[] arrValues = GenerateRandomValuesForVector<Byte>(); |
| 26 | + var spanValues = new Span<Byte>(arrValues); |
| 27 | + foreach (var iteration in Benchmark.Iterations) |
| 28 | + { |
| 29 | + using (iteration.StartMeasurement()) |
| 30 | + { |
| 31 | + Construct<Byte>(spanValues); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 37 | + public static void ConstructorBenchmark_SByte() |
| 38 | + { |
| 39 | + SByte[] arrValues = GenerateRandomValuesForVector<SByte>(); |
| 40 | + var spanValues = new Span<SByte>(arrValues); |
| 41 | + foreach (var iteration in Benchmark.Iterations) |
| 42 | + { |
| 43 | + using (iteration.StartMeasurement()) |
| 44 | + { |
| 45 | + Construct<SByte>(spanValues); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 51 | + public static void ConstructorBenchmark_UInt16() |
| 52 | + { |
| 53 | + UInt16[] arrValues = GenerateRandomValuesForVector<UInt16>(); |
| 54 | + var spanValues = new Span<UInt16>(arrValues); |
| 55 | + foreach (var iteration in Benchmark.Iterations) |
| 56 | + { |
| 57 | + using (iteration.StartMeasurement()) |
| 58 | + { |
| 59 | + Construct<UInt16>(spanValues); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 65 | + public static void ConstructorBenchmark_Int16() |
| 66 | + { |
| 67 | + Int16[] arrValues = GenerateRandomValuesForVector<Int16>(); |
| 68 | + var spanValues = new Span<Int16>(arrValues); |
| 69 | + foreach (var iteration in Benchmark.Iterations) |
| 70 | + { |
| 71 | + using (iteration.StartMeasurement()) |
| 72 | + { |
| 73 | + Construct<Int16>(spanValues); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 79 | + public static void ConstructorBenchmark_UInt32() |
| 80 | + { |
| 81 | + UInt32[] arrValues = GenerateRandomValuesForVector<UInt32>(); |
| 82 | + var spanValues = new Span<UInt32>(arrValues); |
| 83 | + foreach (var iteration in Benchmark.Iterations) |
| 84 | + { |
| 85 | + using (iteration.StartMeasurement()) |
| 86 | + { |
| 87 | + Construct<UInt32>(spanValues); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 93 | + public static void ConstructorBenchmark_Int32() |
| 94 | + { |
| 95 | + Int32[] arrValues = GenerateRandomValuesForVector<Int32>(); |
| 96 | + var spanValues = new Span<Int32>(arrValues); |
| 97 | + foreach (var iteration in Benchmark.Iterations) |
| 98 | + { |
| 99 | + using (iteration.StartMeasurement()) |
| 100 | + { |
| 101 | + Construct<Int32>(spanValues); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 107 | + public static void ConstructorBenchmark_UInt64() |
| 108 | + { |
| 109 | + UInt64[] arrValues = GenerateRandomValuesForVector<UInt64>(); |
| 110 | + var spanValues = new Span<UInt64>(arrValues); |
| 111 | + foreach (var iteration in Benchmark.Iterations) |
| 112 | + { |
| 113 | + using (iteration.StartMeasurement()) |
| 114 | + { |
| 115 | + Construct<UInt64>(spanValues); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 121 | + public static void ConstructorBenchmark_Int64() |
| 122 | + { |
| 123 | + Int64[] arrValues = GenerateRandomValuesForVector<Int64>(); |
| 124 | + var spanValues = new Span<Int64>(arrValues); |
| 125 | + foreach (var iteration in Benchmark.Iterations) |
| 126 | + { |
| 127 | + using (iteration.StartMeasurement()) |
| 128 | + { |
| 129 | + Construct<Int64>(spanValues); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 135 | + public static void ConstructorBenchmark_Single() |
| 136 | + { |
| 137 | + Single[] arrValues = GenerateRandomValuesForVector<Single>(); |
| 138 | + var spanValues = new Span<Single>(arrValues); |
| 139 | + foreach (var iteration in Benchmark.Iterations) |
| 140 | + { |
| 141 | + using (iteration.StartMeasurement()) |
| 142 | + { |
| 143 | + Construct<Single>(spanValues); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 149 | + public static void ConstructorBenchmark_Double() |
| 150 | + { |
| 151 | + Double[] arrValues = GenerateRandomValuesForVector<Double>(); |
| 152 | + var spanValues = new Span<Double>(arrValues); |
| 153 | + foreach (var iteration in Benchmark.Iterations) |
| 154 | + { |
| 155 | + using (iteration.StartMeasurement()) |
| 156 | + { |
| 157 | + Construct<Double>(spanValues); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + public static void Construct<T>(Span<T> values) where T : struct |
| 164 | + { |
| 165 | + for (var iteration = 0; iteration < Benchmark.InnerIterationCount; iteration++) |
| 166 | + { |
| 167 | + Vector<T> vect = new Vector<T>(values); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 172 | + public static void SpanCastBenchmark_Byte() |
| 173 | + { |
| 174 | + Byte[] arrValues = GenerateRandomValuesForVector<Byte>(); |
| 175 | + var spanValues = new ReadOnlySpan<Byte>(arrValues); |
| 176 | + foreach (var iteration in Benchmark.Iterations) |
| 177 | + { |
| 178 | + using (iteration.StartMeasurement()) |
| 179 | + { |
| 180 | + SpanCast<Byte>(spanValues); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 186 | + public static void SpanCastBenchmark_SByte() |
| 187 | + { |
| 188 | + SByte[] arrValues = GenerateRandomValuesForVector<SByte>(); |
| 189 | + var spanValues = new ReadOnlySpan<SByte>(arrValues); |
| 190 | + foreach (var iteration in Benchmark.Iterations) |
| 191 | + { |
| 192 | + using (iteration.StartMeasurement()) |
| 193 | + { |
| 194 | + SpanCast<SByte>(spanValues); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 200 | + public static void SpanCastBenchmark_UInt16() |
| 201 | + { |
| 202 | + UInt16[] arrValues = GenerateRandomValuesForVector<UInt16>(); |
| 203 | + var spanValues = new ReadOnlySpan<UInt16>(arrValues); |
| 204 | + foreach (var iteration in Benchmark.Iterations) |
| 205 | + { |
| 206 | + using (iteration.StartMeasurement()) |
| 207 | + { |
| 208 | + SpanCast<UInt16>(spanValues); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 214 | + public static void SpanCastBenchmark_Int16() |
| 215 | + { |
| 216 | + Int16[] arrValues = GenerateRandomValuesForVector<Int16>(); |
| 217 | + var spanValues = new ReadOnlySpan<Int16>(arrValues); |
| 218 | + foreach (var iteration in Benchmark.Iterations) |
| 219 | + { |
| 220 | + using (iteration.StartMeasurement()) |
| 221 | + { |
| 222 | + SpanCast<Int16>(spanValues); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 228 | + public static void SpanCastBenchmark_UInt32() |
| 229 | + { |
| 230 | + UInt32[] arrValues = GenerateRandomValuesForVector<UInt32>(); |
| 231 | + var spanValues = new ReadOnlySpan<UInt32>(arrValues); |
| 232 | + foreach (var iteration in Benchmark.Iterations) |
| 233 | + { |
| 234 | + using (iteration.StartMeasurement()) |
| 235 | + { |
| 236 | + SpanCast<UInt32>(spanValues); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 242 | + public static void SpanCastBenchmark_Int32() |
| 243 | + { |
| 244 | + Int32[] arrValues = GenerateRandomValuesForVector<Int32>(); |
| 245 | + var spanValues = new ReadOnlySpan<Int32>(arrValues); |
| 246 | + foreach (var iteration in Benchmark.Iterations) |
| 247 | + { |
| 248 | + using (iteration.StartMeasurement()) |
| 249 | + { |
| 250 | + SpanCast<Int32>(spanValues); |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 256 | + public static void SpanCastBenchmark_UInt64() |
| 257 | + { |
| 258 | + UInt64[] arrValues = GenerateRandomValuesForVector<UInt64>(); |
| 259 | + var spanValues = new ReadOnlySpan<UInt64>(arrValues); |
| 260 | + foreach (var iteration in Benchmark.Iterations) |
| 261 | + { |
| 262 | + using (iteration.StartMeasurement()) |
| 263 | + { |
| 264 | + SpanCast<UInt64>(spanValues); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 270 | + public static void SpanCastBenchmark_Int64() |
| 271 | + { |
| 272 | + Int64[] arrValues = GenerateRandomValuesForVector<Int64>(); |
| 273 | + var spanValues = new ReadOnlySpan<Int64>(arrValues); |
| 274 | + foreach (var iteration in Benchmark.Iterations) |
| 275 | + { |
| 276 | + using (iteration.StartMeasurement()) |
| 277 | + { |
| 278 | + SpanCast<Int64>(spanValues); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 284 | + public static void SpanCastBenchmark_Single() |
| 285 | + { |
| 286 | + Single[] arrValues = GenerateRandomValuesForVector<Single>(); |
| 287 | + var spanValues = new ReadOnlySpan<Single>(arrValues); |
| 288 | + foreach (var iteration in Benchmark.Iterations) |
| 289 | + { |
| 290 | + using (iteration.StartMeasurement()) |
| 291 | + { |
| 292 | + SpanCast<Single>(spanValues); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + [Benchmark(InnerIterationCount = DefaultInnerIterationsCount)] |
| 298 | + public static void SpanCastBenchmark_Double() |
| 299 | + { |
| 300 | + Double[] arrValues = GenerateRandomValuesForVector<Double>(); |
| 301 | + var spanValues = new ReadOnlySpan<Double>(arrValues); |
| 302 | + foreach (var iteration in Benchmark.Iterations) |
| 303 | + { |
| 304 | + using (iteration.StartMeasurement()) |
| 305 | + { |
| 306 | + SpanCast<Double>(spanValues); |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + |
| 312 | + public static void SpanCast<T>(ReadOnlySpan<T> values) where T : struct |
| 313 | + { |
| 314 | + for (var iteration = 0; iteration < Benchmark.InnerIterationCount; iteration++) |
| 315 | + { |
| 316 | + ReadOnlySpan<Vector<T>> vectors = MemoryMarshal.Cast<T, Vector<T>>(values); |
| 317 | + Vector<T> vector = vectors[0]; |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + internal static T[] GenerateRandomValuesForVector<T>() where T : struct |
| 322 | + { |
| 323 | + int minValue = GetMinValue<T>(); |
| 324 | + int maxValue = GetMaxValue<T>(); |
| 325 | + return Util.GenerateRandomValues<T>(Vector<T>.Count, minValue, maxValue); |
| 326 | + } |
| 327 | + |
| 328 | + internal static int GetMinValue<T>() where T : struct |
| 329 | + { |
| 330 | + if (typeof(T) == typeof(Int64) || typeof(T) == typeof(Single) || typeof(T) == typeof(Double) || typeof(T) == typeof(UInt32) || typeof(T) == typeof(UInt64)) |
| 331 | + { |
| 332 | + return int.MinValue; |
| 333 | + } |
| 334 | + var typeInfo = typeof(T).GetTypeInfo(); |
| 335 | + var field = typeInfo.GetDeclaredField("MinValue"); |
| 336 | + var value = field.GetValue(null); |
| 337 | + return (int)(dynamic)value; |
| 338 | + } |
| 339 | + |
| 340 | + internal static int GetMaxValue<T>() where T : struct |
| 341 | + { |
| 342 | + if (typeof(T) == typeof(Int64) || typeof(T) == typeof(Single) || typeof(T) == typeof(Double) || typeof(T) == typeof(UInt32) || typeof(T) == typeof(UInt64)) |
| 343 | + { |
| 344 | + return int.MaxValue; |
| 345 | + } |
| 346 | + var typeInfo = typeof(T).GetTypeInfo(); |
| 347 | + var field = typeInfo.GetDeclaredField("MaxValue"); |
| 348 | + var value = field.GetValue(null); |
| 349 | + return (int)(dynamic)value; |
| 350 | + } |
| 351 | + |
| 352 | + } |
| 353 | +} |
0 commit comments