From 804ccb9a9196ef9914f30bf221ae2ecc0a1d14d0 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Fri, 24 Feb 2017 22:04:45 -0800 Subject: [PATCH 1/7] wip --- .../Performance/CodeQuality/Span/SpanBench.cs | 1577 +++++++++++++---- 1 file changed, 1267 insertions(+), 310 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index 7ec2a4a0881d..068bb667e7d3 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -2,467 +2,1424 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Microsoft.Xunit.Performance; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; using Xunit; +using Microsoft.Xunit.Performance; [assembly: OptimizeForBenchmarks] [assembly: MeasureInstructionsRetired] -class Tests +namespace Span { + public class SpanBench + { #if DEBUG - const int Iterations = 1; + const int BubbleSortIterations = 1; + const int QuickSortIterations = 1; + const int FillAllIterations = 1; #else - const int Iterations = 10000; -#endif + const int BubbleSortIterations = 1000; + const int QuickSortIterations = 10000; + const int FillAllIterations = 1000000; + #endif - const int Size = 1024; + const int Size = 1024; + const bool OUTPUT = false; + const int BaseIterations = 100000000; - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestFillAllSpan(Span span) - { - for (int i = 0; i < span.Length; ++i) { - span[i] = unchecked((byte)i); + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestFillAllSpan(Span span) + { + for (int i = 0; i < span.Length; ++i) { + span[i] = unchecked((byte)i); + } } - } - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestFillAllArray(byte[] data) - { - for (int i = 0; i < data.Length; ++i) { - data[i] = unchecked((byte)i); + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestFillAllArray(byte[] data) + { + for (int i = 0; i < data.Length; ++i) { + data[i] = unchecked((byte)i); + } } - } - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestFillAllReverseSpan(Span span) - { - for (int i = span.Length; --i >= 0;) { - span[i] = unchecked((byte)i); + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestFillAllReverseSpan(Span span) + { + for (int i = span.Length; --i >= 0;) { + span[i] = unchecked((byte)i); + } } - } - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestFillAllReverseArray(byte[] data) - { - for (int i = data.Length; --i >= 0;) { - data[i] = unchecked((byte)i); + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestFillAllReverseArray(byte[] data) + { + for (int i = data.Length; --i >= 0;) { + data[i] = unchecked((byte)i); + } } - } - static int[] GetUnsortedData() - { - int[] unsortedData = new int[Size]; - Random r = new Random(42); - for (int i = 0; i < unsortedData.Length; ++i) + static int[] GetUnsortedData() { - unsortedData[i] = r.Next(); + int[] unsortedData = new int[Size]; + Random r = new Random(42); + for (int i = 0; i < unsortedData.Length; ++i) + { + unsortedData[i] = r.Next(); + } + return unsortedData; } - return unsortedData; - } - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestBubbleSortSpan(Span span) - { - bool swap; - int temp; - int n = span.Length - 1; - do { - swap = false; - for (int i = 0; i < n; i++) { - if (span[i] > span[i + 1]) { - temp = span[i]; - span[i] = span[i + 1]; - span[i + 1] = temp; - swap = true; - } - } - --n; - } - while (swap); - } + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestBubbleSortSpan(Span span) + { + bool swap; + int temp; + int n = span.Length - 1; + do { + swap = false; + for (int i = 0; i < n; i++) { + if (span[i] > span[i + 1]) { + temp = span[i]; + span[i] = span[i + 1]; + span[i + 1] = temp; + swap = true; + } + } + --n; + } + while (swap); + } - [MethodImpl(MethodImplOptions.NoInlining)] - static void TestBubbleSortArray(int[] data) - { - bool swap; - int temp; - int n = data.Length - 1; - do { - swap = false; - for (int i = 0; i < n; i++) { - if (data[i] > data[i + 1]) { - temp = data[i]; - data[i] = data[i + 1]; - data[i + 1] = temp; - swap = true; + [MethodImpl(MethodImplOptions.NoInlining)] + static void TestBubbleSortArray(int[] data) + { + bool swap; + int temp; + int n = data.Length - 1; + do { + swap = false; + for (int i = 0; i < n; i++) { + if (data[i] > data[i + 1]) { + temp = data[i]; + data[i] = data[i + 1]; + data[i + 1] = temp; + swap = true; + } } + --n; } - --n; + while (swap); } - while (swap); - } - static void TestQuickSortSpan(Span data) - { - QuickSortSpan(data); - } + static void TestQuickSortSpan(Span data) + { + QuickSortSpan(data); + } - [MethodImpl(MethodImplOptions.NoInlining)] - static void QuickSortSpan(Span data) - { - if (data.Length <= 1) { - return; + [MethodImpl(MethodImplOptions.NoInlining)] + static void QuickSortSpan(Span data) + { + if (data.Length <= 1) { + return; + } + + int lo = 0; + int hi = data.Length - 1; + int i, j; + int pivot, temp; + for (i = lo, j = hi, pivot = data[hi]; i < j;) { + while (i < j && data[i] <= pivot) { + ++i; + } + while (j > i && data[j] >= pivot) { + --j; + } + if (i < j) { + temp = data[i]; + data[i] = data[j]; + data[j] = temp; + } + } + if (i != hi) { + temp = data[i]; + data[i] = pivot; + data[hi] = temp; + } + + QuickSortSpan(data.Slice(0, i)); + QuickSortSpan(data.Slice(i + 1)); + } + + static void TestQuickSortArray(int[] data) + { + QuickSortArray(data, 0, data.Length - 1); } - int lo = 0; - int hi = data.Length - 1; - int i, j; - int pivot, temp; - for (i = lo, j = hi, pivot = data[hi]; i < j;) { - while (i < j && data[i] <= pivot) { - ++i; + [MethodImpl(MethodImplOptions.NoInlining)] + static void QuickSortArray(int[] data, int lo, int hi) + { + if (lo >= hi) { + return; } - while (j > i && data[j] >= pivot) { - --j; + + int i, j; + int pivot, temp; + for (i = lo, j = hi, pivot = data[hi]; i < j;) { + while (i < j && data[i] <= pivot) { + ++i; + } + while (j > i && data[j] >= pivot) { + --j; + } + if (i < j) { + temp = data[i]; + data[i] = data[j]; + data[j] = temp; + } } - if (i < j) { + if (i != hi) { temp = data[i]; - data[i] = data[j]; - data[j] = temp; + data[i] = pivot; + data[hi] = temp; } + + QuickSortArray(data, lo, i - 1); + QuickSortArray(data, i + 1, hi); } - if (i != hi) { - temp = data[i]; - data[i] = pivot; - data[hi] = temp; + + // XUNIT-PERF tests + + /*[Benchmark] + public static void FillAllSpan() + { + byte[] a = new byte[Size]; + Span s = new Span(a); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllSpan(s); + } + } + } } - QuickSortSpan(data.Slice(0, i)); - QuickSortSpan(data.Slice(i + 1)); - } + [Benchmark] + public static void FillAllArray() + { + byte[] a = new byte[Size]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllArray(a); + } + } + } + } - static void TestQuickSortArray(int[] data) - { - QuickSortArray(data, 0, data.Length - 1); - } + [Benchmark] + public static void FillAllReverseSpan() + { + byte[] a = new byte[Size]; + Span s = new Span(a); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllReverseSpan(s); + } + } + } + } - [MethodImpl(MethodImplOptions.NoInlining)] - static void QuickSortArray(int[] data, int lo, int hi) - { - if (lo >= hi) { - return; + [Benchmark] + public static void FillAllReverseArray() + { + byte[] a = new byte[Size]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllReverseArray(a); + } + } + } } - int i, j; - int pivot, temp; - for (i = lo, j = hi, pivot = data[hi]; i < j;) { - while (i < j && data[i] <= pivot) { - ++i; + [Benchmark] + public static void QuickSortSpan() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); + + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < QuickSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestQuickSortSpan(span); + } + } } - while (j > i && data[j] >= pivot) { - --j; + } + + [Benchmark] + public static void BubbleSortSpan() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); + + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < BubbleSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestBubbleSortSpan(span); + } + } } - if (i < j) { - temp = data[i]; - data[i] = data[j]; - data[j] = temp; + } + + [Benchmark] + public static void QuickSortArray() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < QuickSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestQuickSortArray(data); + } + } } } - if (i != hi) { - temp = data[i]; - data[i] = pivot; - data[hi] = temp; + + [Benchmark] + public static void BubbleSortArray() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < BubbleSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestBubbleSortArray(data); + } + } + } + }*/ + + // EXE-based testing + + static void FillAllSpanBase() + { + byte[] a = new byte[Size]; + Span s = new Span(a); + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllSpan(s); + } } - QuickSortArray(data, lo, i - 1); - QuickSortArray(data, i + 1, hi); - } + static void FillAllArrayBase() + { + byte[] a = new byte[Size]; + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllArray(a); + } + } - // XUNIT-PERF tests + static void FillAllReverseSpanBase() + { + byte[] a = new byte[Size]; + Span s = new Span(a); + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllReverseSpan(s); + } + } - [Benchmark] - public static void FillAllSpan() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - foreach (var iteration in Benchmark.Iterations) + static void FillAllReverseArrayBase() + { + byte[] a = new byte[Size]; + for (int i = 0; i < FillAllIterations; i++) + { + TestFillAllReverseArray(a); + } + } + + static void QuickSortSpanBase() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); + + for (int i = 0; i < QuickSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestQuickSortSpan(span); + } + } + + static void BubbleSortSpanBase() { - using (iteration.StartMeasurement()) + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); + + for (int i = 0; i < BubbleSortIterations; i++) { - for (int i = 0; i < Iterations; i++) + Array.Copy(unsortedData, data, Size); + TestBubbleSortSpan(span); + } + } + + static void QuickSortArrayBase() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + + for (int i = 0; i < QuickSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestQuickSortArray(data); + } + } + + static void BubbleSortArrayBase() + { + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + + for (int i = 0; i < BubbleSortIterations; i++) + { + Array.Copy(unsortedData, data, Size); + TestBubbleSortArray(data); + } + } + + static double Bench(Action f) + { + Stopwatch sw = Stopwatch.StartNew(); + f(); + sw.Stop(); + return sw.Elapsed.TotalMilliseconds; + } + + static IEnumerable MakeArgs(params string[] args) + { + return args.Select(arg => new object[] { arg }); + } + + static IEnumerable TestFuncs = MakeArgs( + "FillAllSpanBase", "FillAllArrayBase", + "FillAllReverseSpanBase", "FillAllReverseArrayBase", + "BubbleSortSpanBase", "BubbleSortArrayBase", + "QuickSortSpanBase", "QuickSortArrayBase" + ); + + static Action LookupFunc(object o) + { + TypeInfo t = typeof(SpanBench).GetTypeInfo(); + MethodInfo m = t.GetDeclaredMethod((string) o); + return m.CreateDelegate(typeof(Action)) as Action; + } + + #region TestSpan + /* + private static void TestSpanConstructor() + { + TestSpanConstructor(typeof(byte)); + TestSpanConstructor(); + TestSpanConstructor(); + TestSpanConstructor(); + } + + private static void TestSpanDangerousCreate() + { + TestSpanDangerousCreate(); + TestSpanDangerousCreate(); + TestSpanDangerousCreate(); + TestSpanDangerousCreate(); + } + + private static void TestSpanDangerousGetPinnableReference() + { + TestSpanDangerousGetPinnableReference(); + TestSpanDangerousGetPinnableReference(); + TestSpanDangerousGetPinnableReference(); + TestSpanDangerousGetPinnableReference(); + } + + private static void TestSpanIndex() + { + TestSpanIndex(); + TestSpanIndex(); + TestSpanIndex(); + TestSpanIndex(); + } + + private static void TestSpanSlice() + { + TestSpanSlice(); + TestSpanSlice(); + TestSpanSlice(); + TestSpanSlice(); + } + + private static void TestSpanToArray() + { + TestSpanToArray(); + TestSpanToArray(); + TestSpanToArray(); + TestSpanToArray(); + } + + private static void TestSpanCopyTo() + { + TestSpanCopyTo(); + TestSpanCopyTo(); + TestSpanCopyTo(); + TestSpanCopyTo(); + } + + private static void TestSpanFill() + { + TestSpanFill(); + TestSpanFill(); + TestSpanFill(); + TestSpanFill(); + } + + private static void TestSpanClear() + { + TestSpanClear(); + TestSpanClear(); + TestSpanClear(); + TestSpanClear(); + } + + private static void TestSpanAsBytes() + { + TestSpanAsBytes(); + TestSpanAsBytes(); + } + + private static void TestSpanNonPortableCast() + { + TestSpanNonPortableCast(); + TestSpanNonPortableCast(); + } + + private static void TestSpanSliceString() + { + TestSpanSliceStringChar(); + }*/ + #endregion + + #region TestSpan + [Benchmark(InnerIterationCount = 1000)] + [InlineData(1, BaseIterations * 10)] + [InlineData(5, BaseIterations * 10)] + [InlineData(10, BaseIterations * 10)] + [InlineData(100, BaseIterations * 10)] + [InlineData(500, BaseIterations * 10)] + [InlineData(1000, BaseIterations * 10)] + [InlineData(10000, BaseIterations * 10)] + [InlineData(10000000, BaseIterations * 10)] + public static void TestSpanConstructor(int length, int innerIter) + { + var array = new byte[length]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) { - TestFillAllSpan(s); + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + PerfTestConstructor(array, innerIter); } } } - } - [Benchmark] - public static void FillAllArray() - { - byte[] a = new byte[Size]; - foreach (var iteration in Benchmark.Iterations) + /*[Benchmark] + public static void TestSpanDangerousCreate() { - using (iteration.StartMeasurement()) + TestClass[] classes = GetClasses(); + int[] iterations = GetIterationsForConstructor(); + + for (int i = 0; i < classes.Length; i++) { - for (int i = 0; i < Iterations; i++) + TestClass aClass = classes[i]; + int innerIter = iterations[i]; + + foreach (var iteration in Benchmark.Iterations) { - TestFillAllArray(a); + using (iteration.StartMeasurement()) + { + PerfTestDangerousCreate(aClass, innerIter); + } } } } - } - [Benchmark] - public static void FillAllReverseSpan() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - foreach (var iteration in Benchmark.Iterations) + [Benchmark] + public static void TestSpanDangerousGetPinnableReference() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForDangerousGetPinnableReference(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + + foreach (var iteration in Benchmark.Iterations) { - TestFillAllReverseSpan(s); + using (iteration.StartMeasurement()) + { + PerfTestDangerousGetPinnableReference(span, innerIter); + } } } } - } - [Benchmark] - public static void FillAllReverseArray() - { - byte[] a = new byte[Size]; - foreach (var iteration in Benchmark.Iterations) + [Benchmark] + public static void TestSpanIndex() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForIndex(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + + foreach (var iteration in Benchmark.Iterations) { - TestFillAllReverseArray(a); + using (iteration.StartMeasurement()) + { + PerfTestIndex(span, innerIter); + } } } } - } - [Benchmark] - public static void QuickSortSpan() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); + [Benchmark] + public static void TestArrayIndex() + { + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForIndex(); - foreach (var iteration in Benchmark.Iterations) + for (int i = 0; i < arrays.Length; i++) + { + var array = arrays[i]; + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestIndex(array, innerIter); + } + } + } + } + + [Benchmark] + public static void TestSpanSlice() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForSlice(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + + foreach (var iteration in Benchmark.Iterations) { - Array.Copy(unsortedData, data, Size); - TestQuickSortSpan(span); + using (iteration.StartMeasurement()) + { + PerfTestSlice(span, innerIter); + } } } } - } - [Benchmark] - public static void BubbleSortSpan() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); + [Benchmark] + public static void TestSpanToArray() + { + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForToArray(); + + for (int i = 0; i < arrays.Length; i++) + { + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestToArray(span, innerIter); + } + } + } + } - foreach (var iteration in Benchmark.Iterations) + [Benchmark] + public static void TestSpanCopyTo() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForCopyTo(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + Span span = new Span(arrays[i]); + var destArray = new T[arrays[i].Length]; + Span destination = new Span(destArray); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) { - Array.Copy(unsortedData, data, Size); - TestBubbleSortSpan(span); + using (iteration.StartMeasurement()) + { + PerfTestCopyTo(span, destination, innerIter); + } } } } - } - [Benchmark] - public static void QuickSortArray() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); + [Benchmark] + public static void TestArrayCopyTo() + { + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForCopyTo(); - foreach (var iteration in Benchmark.Iterations) + for (int i = 0; i < arrays.Length; i++) + { + var array = arrays[i]; + var destArray = new T[arrays[i].Length]; + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestCopyTo(array, destArray, innerIter); + } + } + } + } + + [Benchmark] + public static void TestSpanFill() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForFill(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) { - Array.Copy(unsortedData, data, Size); - TestQuickSortArray(data); + using (iteration.StartMeasurement()) + { + PerfTestFill(span, innerIter); + } } } } - } - [Benchmark] - public static void BubbleSortArray() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); + [Benchmark] + public static void TestSpanClear() + { + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForClear(); + + for (int i = 0; i < arrays.Length; i++) + { + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestClear(span, innerIter); + } + } + } + } - foreach (var iteration in Benchmark.Iterations) + [Benchmark] + public static void TestArrayClear() { - using (iteration.StartMeasurement()) + T[][] arrays = GetArrays(); + int[] iterations = GetIterationsForClear(); + + for (int i = 0; i < arrays.Length; i++) { - for (int i = 0; i < Iterations; i++) + var array = arrays[i]; + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) { - Array.Copy(unsortedData, data, Size); - TestBubbleSortArray(data); + using (iteration.StartMeasurement()) + { + PerfTestClear(array, innerIter); + } } } } - } - // EXE-based testing + [Benchmark] + public static void TestSpanAsBytes() + where T : struct + { + T[][] arrays = GetArrays(); + int[] iterations = GetIterations(); - static void FillAllSpanBase() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - for (int i = 0; i < Iterations; i++) + for (int i = 0; i < arrays.Length; i++) + { + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestAsBytes(span, innerIter); + } + } + } + } + + [Benchmark] + public static void TestSpanNonPortableCast() + where TFrom : struct + where TTo : struct { - TestFillAllSpan(s); + TFrom[][] arrays = GetArrays(); + int[] iterations = GetIterations(); + + for (int i = 0; i < arrays.Length; i++) + { + Span span = new Span(arrays[i]); + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestNonPortableCast(span, innerIter); + } + } + } } - } - static void FillAllArrayBase() - { - byte[] a = new byte[Size]; - for (int i = 0; i < Iterations; i++) + [Benchmark] + public static void TestSpanSliceStringChar() { - TestFillAllArray(a); + string[] strings = GetStrings(); + int[] iterations = GetIterations(); + + for (int i = 0; i < strings.Length; i++) + { + string s = strings[i]; + int innerIter = iterations[i]; + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + PerfTestSliceString(s, innerIter); + } + } + } + }*/ + #endregion + + #region PerfTest + private static void PerfTestConstructor(T[] array, int iterations) + { + for (int j = 0; j < iterations; j++) + { + Span span = new Span(array); + } } - } - static void FillAllReverseSpanBase() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - for (int i = 0; i < Iterations; i++) + private static void PerfTestDangerousCreate(TestClass testClass, int iterations) { - TestFillAllReverseSpan(s); + for (int j = 0; j < iterations; j++) + { + Span span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); + } } - } - static void FillAllReverseArrayBase() - { - byte[] a = new byte[Size]; - for (int i = 0; i < Iterations; i++) + /* + private static void PerfTestDangerousGetPinnableReference(Span span, int iterations) { - TestFillAllReverseArray(a); + for (int j = 0; j < iterations; j++) + { + ref T temp = ref span.DangerousGetPinnableReference(); + } + }*/ + + private static void PerfTestIndex(Span span, int iterations) + { + for (int j = 0; j < iterations; j++) + { + for (int i = 0; i < span.Length; i++) + { + var temp = span[i]; + //var temp = span.GetItem(i); + } + } } - } - static void QuickSortSpanBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); + private static void PerfTestSlice(Span span, int iterations) + { + for (int j = 0; j < iterations; j++) + { + for (int i = 0; i < span.Length; i++) + { + var temp = span.Slice(i); + } + } + } - for (int i = 0; i < Iterations; i++) + private static void PerfTestToArray(Span span, int iterations) { - Array.Copy(unsortedData, data, Size); - TestQuickSortSpan(span); + for (int j = 0; j < iterations; j++) + { + var temp = span.ToArray(); + } } - } - static void BubbleSortSpanBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); + private static void PerfTestCopyTo(Span span, Span destination, int iterations) + { + for (int j = 0; j < iterations; j++) + { + span.CopyTo(destination); + } + } - for (int i = 0; i < Iterations; i++) + private static void PerfTestFill(Span span, int iterations) { - Array.Copy(unsortedData, data, Size); - TestBubbleSortSpan(span); + for (int j = 0; j < iterations; j++) + { + span.Fill(default(T)); + } } - } - static void QuickSortArrayBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); + private static void PerfTestClear(Span span, int iterations) + { + for (int j = 0; j < iterations; j++) + { + span.Clear(); + } + } - for (int i = 0; i < Iterations; i++) + private static void PerfTestAsBytes(Span span, int iterations) + where T : struct { - Array.Copy(unsortedData, data, Size); - TestQuickSortArray(data); + for (int j = 0; j < iterations; j++) + { + Span temp = span.AsBytes(); + } } - } - static void BubbleSortArrayBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); + private static void PerfTestNonPortableCast(Span span, int iterations) + where TFrom : struct + where TTo : struct + { + for (int j = 0; j < iterations; j++) + { + Span temp = span.NonPortableCast(); + } + } - for (int i = 0; i < Iterations; i++) + private static void PerfTestSliceString(string s, int iterations) { - Array.Copy(unsortedData, data, Size); - TestBubbleSortArray(data); + for (int j = 0; j < iterations; j++) + { + ReadOnlySpan temp = s.Slice(); + } } - } + #endregion - static double Bench(Action f) - { - Stopwatch sw = Stopwatch.StartNew(); - f(); - sw.Stop(); - return sw.Elapsed.TotalMilliseconds; - } + #region PerfTest(Array) + private static void PerfTestIndex(T[] array, int iterations) + { + for (int j = 0; j < iterations; j++) + { + for (int i = 0; i < array.Length; i++) + { + var temp = array[i]; + //var temp = span.GetItem(i); + } + } + } - static IEnumerable MakeArgs(params string[] args) - { - return args.Select(arg => new object[] { arg }); - } + private static void PerfTestCopyTo(T[] array, T[] destination, int iterations) + { + for (int j = 0; j < iterations; j++) + { + array.CopyTo(destination, 0); + } + } - static IEnumerable TestFuncs = MakeArgs( - "FillAllSpanBase", "FillAllArrayBase", - "FillAllReverseSpanBase", "FillAllReverseArrayBase", - "BubbleSortSpanBase", "BubbleSortArrayBase", - "QuickSortSpanBase", "QuickSortArrayBase" - ); + private static void PerfTestClear(T[] array, int iterations) + { + int length = array.Length; + for (int j = 0; j < iterations; j++) + { + Array.Clear(array, 0, length); + } + } + #endregion - static Action LookupFunc(object o) - { - TypeInfo t = typeof(Tests).GetTypeInfo(); - MethodInfo m = t.GetDeclaredMethod((string) o); - return m.CreateDelegate(typeof(Action)) as Action; - } + #region GetIterationsFor + private static int[] GetIterationsForConstructor() + { + int[] iterations = new int[8]; - public static int Main(string[] args) - { - bool result = true; + if (typeof(T) == typeof(string)) + { + iterations[0] = BaseIterations / 10; + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 10; + iterations[7] = BaseIterations / 10; + } + else + { + iterations[0] = BaseIterations * 10; + iterations[1] = BaseIterations * 10; + iterations[2] = BaseIterations * 10; + iterations[3] = BaseIterations * 10; + iterations[4] = BaseIterations * 10; + iterations[5] = BaseIterations * 10; + iterations[6] = BaseIterations * 10; + iterations[7] = BaseIterations * 10; + } + + return iterations; + } - foreach(object[] o in TestFuncs) + private static int[] GetIterationsForDangerousGetPinnableReference() { - string funcName = (string) o[0]; - Action func = LookupFunc(funcName); - double timeInMs = Bench(func); - Console.WriteLine("{0}: {1}ms", funcName, timeInMs); + int[] iterations = new int[8]; + + iterations[0] = BaseIterations * 10; + iterations[1] = BaseIterations * 10; + iterations[2] = BaseIterations * 10; + iterations[3] = BaseIterations * 10; + iterations[4] = BaseIterations * 10; + iterations[5] = BaseIterations * 10; + iterations[6] = BaseIterations * 10; + iterations[7] = BaseIterations * 10; + + return iterations; } - return (result ? 100 : -1); - } -} + private static int[] GetIterationsForIndex() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + if (typeof(T) == typeof(string)) + { + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 1000; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + } + else + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 100; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + + return iterations; + } + private static int[] GetIterationsForSlice() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 1000; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + + return iterations; + } + + private static int[] GetIterationsForToArray() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + if (typeof(T) == typeof(byte)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 100; + iterations[7] = BaseIterations / 1000000; + } + else if(typeof(T) == typeof(int)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 100; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + else + { + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 1000; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + } + + return iterations; + } + + private static int[] GetIterationsForCopyTo() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + if (typeof(T) == typeof(byte) || (typeof(T) == typeof(int))) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 100; + iterations[7] = BaseIterations / 1000000; + } + else + { + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + } + + return iterations; + } + + private static int[] GetIterationsForFill() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + if (typeof(T) == typeof(byte)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 100; + iterations[7] = BaseIterations / 100000; + } + else if (typeof(T) == typeof(string)) + { + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 1000; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + } + else if (typeof(T) == typeof(TestStruct)) + { + iterations[1] = BaseIterations / 10; + iterations[2] = BaseIterations / 10; + iterations[3] = BaseIterations / 100; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 1000; + iterations[6] = BaseIterations / 10000; + iterations[7] = BaseIterations / 10000000; + } + else + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 100; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + + return iterations; + } + + private static int[] GetIterationsForClear() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + if (typeof(T) == typeof(byte)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 100; + iterations[7] = BaseIterations / 100000; + } + else if (typeof(T) == typeof(string)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 100; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + else if (typeof(T) == typeof(TestStruct)) + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 100; + iterations[5] = BaseIterations / 100; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + else + { + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations / 10; + iterations[4] = BaseIterations / 10; + iterations[5] = BaseIterations / 10; + iterations[6] = BaseIterations / 1000; + iterations[7] = BaseIterations / 1000000; + } + + return iterations; + } + + private static int[] GetIterations() + { + int[] iterations = new int[8]; + + iterations[0] = BaseIterations; + iterations[1] = BaseIterations; + iterations[2] = BaseIterations; + iterations[3] = BaseIterations; + iterations[4] = BaseIterations; + iterations[5] = BaseIterations; + iterations[6] = BaseIterations; + iterations[7] = BaseIterations; + + return iterations; + } + #endregion + + #region Helpers + private static T[][] GetArrays() + { + T[][] arrays = new T[8][]; + + arrays[0] = new T[1]; + arrays[1] = new T[5]; + arrays[2] = new T[10]; + arrays[3] = new T[100]; + arrays[4] = new T[500]; + arrays[5] = new T[1000]; + arrays[6] = new T[10000]; + arrays[7] = new T[10000000]; + + return arrays; + } + + private static string[] GetStrings() + { + string[] strings = new string[8]; + Random rand = new Random(42); + + strings[0] = ""; + strings[1] = ""; + strings[2] = ""; + strings[3] = ""; + strings[4] = ""; + strings[5] = ""; + strings[6] = ""; + strings[7] = ""; + + StringBuilder sb = new StringBuilder(); + char[] c = new char[1]; + for (int i = 0; i < 10000000; i++) + { + c[0] = (char)rand.Next(32, 126); + sb.Append(new string(c)); + if (i == 1-1) + { + strings[0] = sb.ToString(); + } + if (i == 5-1) + { + strings[1] = sb.ToString(); + } + if (i == 10-1) + { + strings[2] = sb.ToString(); + } + if (i == 100-1) + { + strings[3] = sb.ToString(); + } + if (i == 500-1) + { + strings[4] = sb.ToString(); + } + if (i == 1000-1) + { + strings[5] = sb.ToString(); + } + if (i == 10000-1) + { + strings[6] = sb.ToString(); + } + } + strings[7] = sb.ToString(); + + return strings; + } + + private static TestClass[] GetClasses() + { + TestClass[] classes = new TestClass[8]; + + classes[0] = new TestClass(); + classes[1] = new TestClass(); + classes[2] = new TestClass(); + classes[3] = new TestClass(); + classes[4] = new TestClass(); + classes[5] = new TestClass(); + classes[6] = new TestClass(); + classes[7] = new TestClass(); + + classes[0].C0 = new T[1]; + classes[1].C0 = new T[5]; + classes[2].C0 = new T[10]; + classes[3].C0 = new T[100]; + classes[4].C0 = new T[500]; + classes[5].C0 = new T[1000]; + classes[6].C0 = new T[10000]; + classes[7].C0 = new T[10000000]; + + return classes; + } + + [StructLayout(LayoutKind.Sequential)] + private sealed class TestClass + { + private double _d; + public T[] C0; + } + + [StructLayout(LayoutKind.Sequential)] + private struct TestStruct + { + public int I; + public string S; + } + #endregion + + public static int Main(string[] args) + { + bool result = true; + + /*foreach(object[] o in TestFuncs) + { + string funcName = (string) o[0]; + Action func = LookupFunc(funcName); + double timeInMs = Bench(func); + Console.WriteLine("{0}: {1}ms", funcName, timeInMs); + }*/ + +/*#if !DEBUG + TestSpanConstructor(); + TestSpanDangerousCreate(); + TestSpanDangerousGetPinnableReference(); + TestSpanIndex(); + TestSpanSlice(); + TestSpanToArray(); + TestSpanCopyTo(); + TestSpanFill(); + TestSpanClear(); + TestSpanAsBytes(); + TestSpanNonPortableCast(); + TestSpanSliceString(); +#endif*/ + + return (result ? 100 : -1); + } + } +} \ No newline at end of file From f55346a8819379f8e69afd7cf42248dbece3b281 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Sat, 25 Feb 2017 01:51:43 -0800 Subject: [PATCH 2/7] Adding Span API perf tests for comparison with arrays and to monitor progress --- .../Performance/CodeQuality/Span/SpanBench.cs | 2008 +++++++++-------- 1 file changed, 1085 insertions(+), 923 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index 068bb667e7d3..bd2df25b9542 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -25,16 +25,31 @@ public class SpanBench const int BubbleSortIterations = 1; const int QuickSortIterations = 1; const int FillAllIterations = 1; + const int BaseIterations = 1; #else const int BubbleSortIterations = 1000; const int QuickSortIterations = 10000; const int FillAllIterations = 1000000; - #endif + const int BaseIterations = 100; +#endif const int Size = 1024; - const bool OUTPUT = false; - const int BaseIterations = 100000000; + // Helpers + #region Helpers + [StructLayout(LayoutKind.Sequential)] + private sealed class TestClass + { + private double _d; + public T[] C0; + } + + [StructLayout(LayoutKind.Sequential)] + private struct TestStruct + { + public int I; + public string S; + } [MethodImpl(MethodImplOptions.NoInlining)] static void TestFillAllSpan(Span span) @@ -196,10 +211,11 @@ static void QuickSortArray(int[] data, int lo, int hi) QuickSortArray(data, lo, i - 1); QuickSortArray(data, i + 1, hi); } + #endregion // XUNIT-PERF tests - - /*[Benchmark] + #region XUNIT-PERF tests + [Benchmark] public static void FillAllSpan() { byte[] a = new byte[Size]; @@ -341,1084 +357,1230 @@ public static void BubbleSortArray() } } } - }*/ - - // EXE-based testing - - static void FillAllSpanBase() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - for (int i = 0; i < FillAllIterations; i++) - { - TestFillAllSpan(s); - } - } - - static void FillAllArrayBase() - { - byte[] a = new byte[Size]; - for (int i = 0; i < FillAllIterations; i++) - { - TestFillAllArray(a); - } - } - - static void FillAllReverseSpanBase() - { - byte[] a = new byte[Size]; - Span s = new Span(a); - for (int i = 0; i < FillAllIterations; i++) - { - TestFillAllReverseSpan(s); - } - } - - static void FillAllReverseArrayBase() - { - byte[] a = new byte[Size]; - for (int i = 0; i < FillAllIterations; i++) - { - TestFillAllReverseArray(a); - } - } - - static void QuickSortSpanBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); - - for (int i = 0; i < QuickSortIterations; i++) - { - Array.Copy(unsortedData, data, Size); - TestQuickSortSpan(span); - } - } - - static void BubbleSortSpanBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - Span span = new Span(data); - - for (int i = 0; i < BubbleSortIterations; i++) - { - Array.Copy(unsortedData, data, Size); - TestBubbleSortSpan(span); - } - } - - static void QuickSortArrayBase() - { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - - for (int i = 0; i < QuickSortIterations; i++) - { - Array.Copy(unsortedData, data, Size); - TestQuickSortArray(data); - } } + #endregion - static void BubbleSortArrayBase() + // TestSpanAPIs (For comparison with Array and Slow Span) + #region TestSpanAPIs + + #region TestSpanConstructor + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanConstructorByte(int length) { - int[] data = new int[Size]; - int[] unsortedData = GetUnsortedData(); - - for (int i = 0; i < BubbleSortIterations; i++) - { - Array.Copy(unsortedData, data, Size); - TestBubbleSortArray(data); - } + var array = new byte[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = new Span(array); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanConstructorInt(int length) + { + var array = new int[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = new Span(array); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanConstructorString(int length) + { + var array = new string[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = new Span(array); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanConstructorTestStruct(int length) + { + var array = new TestStruct[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = new Span(array); } - - static double Bench(Action f) - { - Stopwatch sw = Stopwatch.StartNew(); - f(); - sw.Stop(); - return sw.Elapsed.TotalMilliseconds; + #endregion + + #region TestSpanDangerousCreate + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousCreateByte(int length) + { + TestClass testClass = new TestClass(); + testClass.C0 = new byte[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousCreateInt(int length) + { + TestClass testClass = new TestClass(); + testClass.C0 = new int[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousCreateString(int length) + { + TestClass testClass = new TestClass(); + testClass.C0 = new string[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousCreateTestStruct(int length) + { + TestClass testClass = new TestClass(); + testClass.C0 = new TestStruct[length]; + Span span; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); } + #endregion - static IEnumerable MakeArgs(params string[] args) + #region TestSpanDangerousGetPinnableReference + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousGetPinnableReferenceByte(int length) { - return args.Select(arg => new object[] { arg }); + var array = new byte[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + ref byte temp = ref span.DangerousGetPinnableReference(); + } } - static IEnumerable TestFuncs = MakeArgs( - "FillAllSpanBase", "FillAllArrayBase", - "FillAllReverseSpanBase", "FillAllReverseArrayBase", - "BubbleSortSpanBase", "BubbleSortArrayBase", - "QuickSortSpanBase", "QuickSortArrayBase" - ); - - static Action LookupFunc(object o) + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousGetPinnableReferenceInt(int length) { - TypeInfo t = typeof(SpanBench).GetTypeInfo(); - MethodInfo m = t.GetDeclaredMethod((string) o); - return m.CreateDelegate(typeof(Action)) as Action; + var array = new int[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + ref int temp = ref span.DangerousGetPinnableReference(); + } } - #region TestSpan - /* - private static void TestSpanConstructor() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousGetPinnableReferenceString(int length) { - TestSpanConstructor(typeof(byte)); - TestSpanConstructor(); - TestSpanConstructor(); - TestSpanConstructor(); + var array = new string[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + ref string temp = ref span.DangerousGetPinnableReference(); + } } - private static void TestSpanDangerousCreate() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) { - TestSpanDangerousCreate(); - TestSpanDangerousCreate(); - TestSpanDangerousCreate(); - TestSpanDangerousCreate(); + var array = new TestStruct[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + ref TestStruct temp = ref span.DangerousGetPinnableReference(); + } } + #endregion - private static void TestSpanDangerousGetPinnableReference() + #region TestSpanIndex + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanIndexByte(int length) { - TestSpanDangerousGetPinnableReference(); - TestSpanDangerousGetPinnableReference(); - TestSpanDangerousGetPinnableReference(); - TestSpanDangerousGetPinnableReference(); - } + var array = new byte[length]; + var span = new Span(array); + byte temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span[length/2]; - private static void TestSpanIndex() - { - TestSpanIndex(); - TestSpanIndex(); - TestSpanIndex(); - TestSpanIndex(); } - private static void TestSpanSlice() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanIndexInt(int length) { - TestSpanSlice(); - TestSpanSlice(); - TestSpanSlice(); - TestSpanSlice(); + var array = new int[length]; + var span = new Span(array); + int temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span[length / 2]; + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanIndexString(int length) + { + var array = new string[length]; + var span = new Span(array); + string temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span[length / 2]; + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanIndexTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + TestStruct temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span[length / 2]; } + #endregion - private static void TestSpanToArray() + #region TestArrayIndex + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestArrayIndexByte(int length) { - TestSpanToArray(); - TestSpanToArray(); - TestSpanToArray(); - TestSpanToArray(); - } + var array = new byte[length]; + byte temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = array[length / 2]; - private static void TestSpanCopyTo() - { - TestSpanCopyTo(); - TestSpanCopyTo(); - TestSpanCopyTo(); - TestSpanCopyTo(); } - private static void TestSpanFill() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestArrayIndexInt(int length) { - TestSpanFill(); - TestSpanFill(); - TestSpanFill(); - TestSpanFill(); + var array = new int[length]; + int temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = array[length / 2]; + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestArrayIndexString(int length) + { + var array = new string[length]; + string temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = array[length / 2]; + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestArrayIndexTestStruct(int length) + { + var array = new TestStruct[length]; + TestStruct temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = array[length / 2]; } + #endregion - private static void TestSpanClear() + #region TestSpanSlice + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanSliceByte(int length) { - TestSpanClear(); - TestSpanClear(); - TestSpanClear(); - TestSpanClear(); - } + var array = new byte[length]; + var span = new Span(array); + Span temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.Slice(length / 2); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanSliceInt(int length) + { + var array = new int[length]; + var span = new Span(array); + Span temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.Slice(length / 2); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanSliceString(int length) + { + var array = new string[length]; + var span = new Span(array); + Span temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.Slice(length / 2); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanSliceTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + Span temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.Slice(length / 2); - private static void TestSpanAsBytes() - { - TestSpanAsBytes(); - TestSpanAsBytes(); } - - private static void TestSpanNonPortableCast() + #endregion + + #region TestSpanToArray + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanToArrayByte(int length) { - TestSpanNonPortableCast(); - TestSpanNonPortableCast(); + var array = new byte[length]; + var span = new Span(array); + byte[] temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.ToArray(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanToArrayInt(int length) + { + var array = new int[length]; + var span = new Span(array); + int[] temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.ToArray(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanToArrayString(int length) + { + var array = new string[length]; + var span = new Span(array); + string[] temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.ToArray(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanToArrayTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + TestStruct[] temp; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + temp = span.ToArray(); } - - private static void TestSpanSliceString() - { - TestSpanSliceStringChar(); - }*/ #endregion - - #region TestSpan - [Benchmark(InnerIterationCount = 1000)] - [InlineData(1, BaseIterations * 10)] - [InlineData(5, BaseIterations * 10)] - [InlineData(10, BaseIterations * 10)] - [InlineData(100, BaseIterations * 10)] - [InlineData(500, BaseIterations * 10)] - [InlineData(1000, BaseIterations * 10)] - [InlineData(10000, BaseIterations * 10)] - [InlineData(10000000, BaseIterations * 10)] - public static void TestSpanConstructor(int length, int innerIter) + + #region TestSpanCopyTo + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanCopyToByte(int length) { var array = new byte[length]; + var span = new Span(array); + var destArray = new byte[array.Length]; + var destination = new Span(destArray); foreach (var iteration in Benchmark.Iterations) - { using (iteration.StartMeasurement()) - { for (int i = 0; i < Benchmark.InnerIterationCount; i++) - PerfTestConstructor(array, innerIter); - } - } + span.CopyTo(destination); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanCopyToInt(int length) + { + var array = new int[length]; + var span = new Span(array); + var destArray = new int[array.Length]; + var destination = new Span(destArray); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.CopyTo(destination); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanCopyToString(int length) + { + var array = new string[length]; + var span = new Span(array); + var destArray = new string[array.Length]; + var destination = new Span(destArray); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.CopyTo(destination); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanCopyToTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + var destArray = new TestStruct[array.Length]; + var destination = new Span(destArray); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.CopyTo(destination); } + #endregion - /*[Benchmark] - public static void TestSpanDangerousCreate() + #region TestArrayCopyTo + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayCopyToByte(int length) { - TestClass[] classes = GetClasses(); - int[] iterations = GetIterationsForConstructor(); - - for (int i = 0; i < classes.Length; i++) - { - TestClass aClass = classes[i]; - int innerIter = iterations[i]; - - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestDangerousCreate(aClass, innerIter); - } - } - } + var array = new byte[length]; + var destination = new byte[array.Length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + array.CopyTo(destination, 0); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayCopyToInt(int length) + { + var array = new int[length]; + var destination = new int[array.Length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + array.CopyTo(destination, 0); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayCopyToString(int length) + { + var array = new string[length]; + var destination = new string[array.Length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + array.CopyTo(destination, 0); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayCopyToTestStruct(int length) + { + var array = new TestStruct[length]; + var destination = new TestStruct[array.Length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + array.CopyTo(destination, 0); } + #endregion - [Benchmark] - public static void TestSpanDangerousGetPinnableReference() + #region TestSpanFill + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanFillByte(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForDangerousGetPinnableReference(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestDangerousGetPinnableReference(span, innerIter); - } - } - } + var array = new byte[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Fill(default(byte)); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanFillInt(int length) + { + var array = new int[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Fill(default(int)); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanFillString(int length) + { + var array = new string[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Fill(default(string)); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanFillTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Fill(default(TestStruct)); } + #endregion - [Benchmark] - public static void TestSpanIndex() + #region TestSpanClear + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanClearByte(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForIndex(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestIndex(span, innerIter); - } - } - } + var array = new byte[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Clear(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanClearInt(int length) + { + var array = new int[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Clear(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanClearString(int length) + { + var array = new string[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Clear(); + } + + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestSpanClearTestStruct(int length) + { + var array = new TestStruct[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + span.Clear(); } + #endregion - [Benchmark] - public static void TestArrayIndex() + #region TestArrayClear + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayClear(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForIndex(); - - for (int i = 0; i < arrays.Length; i++) - { - var array = arrays[i]; - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestIndex(array, innerIter); - } - } - } + var array = new byte[length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + Array.Clear(array, 0, length); } - [Benchmark] - public static void TestSpanSlice() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayClearInt(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForSlice(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestSlice(span, innerIter); - } - } - } + var array = new int[length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + Array.Clear(array, 0, length); } - [Benchmark] - public static void TestSpanToArray() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayClearString(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForToArray(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestToArray(span, innerIter); - } - } - } + var array = new string[length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + Array.Clear(array, 0, length); } - [Benchmark] - public static void TestSpanCopyTo() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(100000)] + public static void TestArrayClearTestStruct(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForCopyTo(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - var destArray = new T[arrays[i].Length]; - Span destination = new Span(destArray); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestCopyTo(span, destination, innerIter); - } - } - } + var array = new TestStruct[length]; + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + Array.Clear(array, 0, length); } + #endregion - [Benchmark] - public static void TestArrayCopyTo() + #region TestSpanAsBytes + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanAsBytesByte(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForCopyTo(); - - for (int i = 0; i < arrays.Length; i++) - { - var array = arrays[i]; - var destArray = new T[arrays[i].Length]; - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) + var array = new byte[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) { - PerfTestCopyTo(array, destArray, innerIter); + Span temp = span.AsBytes(); } - } - } } - [Benchmark] - public static void TestSpanFill() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanAsBytesInt(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForFill(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) + var array = new int[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) { - PerfTestFill(span, innerIter); + Span temp = span.AsBytes(); } - } - } } + #endregion - [Benchmark] - public static void TestSpanClear() + #region TestSpanNonPortableCast + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanNonPortableCastFromByteToInt(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForClear(); - - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) + var array = new byte[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) { - PerfTestClear(span, innerIter); + Span temp = span.NonPortableCast(); } - } - } } - [Benchmark] - public static void TestArrayClear() + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanNonPortableCastFromIntToByte(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterationsForClear(); - - for (int i = 0; i < arrays.Length; i++) - { - var array = arrays[i]; - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) + var array = new int[length]; + var span = new Span(array); + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) { - PerfTestClear(array, innerIter); + Span temp = span.NonPortableCast(); } - } - } } + #endregion - [Benchmark] - public static void TestSpanAsBytes() - where T : struct + #region TestSpanSliceStringChar + [Benchmark(InnerIterationCount = BaseIterations)] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(100)] + [InlineData(500)] + [InlineData(1000)] + [InlineData(10000)] + [InlineData(10000000)] + public static void TestSpanSliceStringChar(int length) { - T[][] arrays = GetArrays(); - int[] iterations = GetIterations(); - - for (int i = 0; i < arrays.Length; i++) + StringBuilder sb = new StringBuilder(); + Random rand = new Random(42); + char[] c = new char[1]; + for (int i = 0; i < length; i++) { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestAsBytes(span, innerIter); - } - } + c[0] = (char)rand.Next(32, 126); + sb.Append(new string(c)); } - } - - [Benchmark] - public static void TestSpanNonPortableCast() - where TFrom : struct - where TTo : struct - { - TFrom[][] arrays = GetArrays(); - int[] iterations = GetIterations(); + string s = sb.ToString(); - for (int i = 0; i < arrays.Length; i++) - { - Span span = new Span(arrays[i]); - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) + foreach (var iteration in Benchmark.Iterations) + using (iteration.StartMeasurement()) + for (int i = 0; i < Benchmark.InnerIterationCount; i++) { - PerfTestNonPortableCast(span, innerIter); + ReadOnlySpan temp = s.Slice(); } - } - } } - - [Benchmark] - public static void TestSpanSliceStringChar() - { - string[] strings = GetStrings(); - int[] iterations = GetIterations(); - - for (int i = 0; i < strings.Length; i++) - { - string s = strings[i]; - int innerIter = iterations[i]; - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - PerfTestSliceString(s, innerIter); - } - } - } - }*/ #endregion - #region PerfTest - private static void PerfTestConstructor(T[] array, int iterations) - { - for (int j = 0; j < iterations; j++) - { - Span span = new Span(array); - } - } - - private static void PerfTestDangerousCreate(TestClass testClass, int iterations) - { - for (int j = 0; j < iterations; j++) - { - Span span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); - } - } - - /* - private static void PerfTestDangerousGetPinnableReference(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - ref T temp = ref span.DangerousGetPinnableReference(); - } - }*/ - - private static void PerfTestIndex(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - for (int i = 0; i < span.Length; i++) - { - var temp = span[i]; - //var temp = span.GetItem(i); - } - } - } - - private static void PerfTestSlice(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - for (int i = 0; i < span.Length; i++) - { - var temp = span.Slice(i); - } - } - } - - private static void PerfTestToArray(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - var temp = span.ToArray(); - } - } - - private static void PerfTestCopyTo(Span span, Span destination, int iterations) - { - for (int j = 0; j < iterations; j++) - { - span.CopyTo(destination); - } - } - - private static void PerfTestFill(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - span.Fill(default(T)); - } - } - - private static void PerfTestClear(Span span, int iterations) - { - for (int j = 0; j < iterations; j++) - { - span.Clear(); - } - } - - private static void PerfTestAsBytes(Span span, int iterations) - where T : struct - { - for (int j = 0; j < iterations; j++) - { - Span temp = span.AsBytes(); - } - } - - private static void PerfTestNonPortableCast(Span span, int iterations) - where TFrom : struct - where TTo : struct - { - for (int j = 0; j < iterations; j++) - { - Span temp = span.NonPortableCast(); - } - } - - private static void PerfTestSliceString(string s, int iterations) - { - for (int j = 0; j < iterations; j++) - { - ReadOnlySpan temp = s.Slice(); - } - } #endregion - - #region PerfTest(Array) - private static void PerfTestIndex(T[] array, int iterations) - { - for (int j = 0; j < iterations; j++) - { - for (int i = 0; i < array.Length; i++) - { - var temp = array[i]; - //var temp = span.GetItem(i); - } - } - } - - private static void PerfTestCopyTo(T[] array, T[] destination, int iterations) + + // EXE-based testing + #region EXE-base testing + static void FillAllSpanBase() { - for (int j = 0; j < iterations; j++) + byte[] a = new byte[Size]; + Span s = new Span(a); + for (int i = 0; i < FillAllIterations; i++) { - array.CopyTo(destination, 0); + TestFillAllSpan(s); } } - private static void PerfTestClear(T[] array, int iterations) + static void FillAllArrayBase() { - int length = array.Length; - for (int j = 0; j < iterations; j++) + byte[] a = new byte[Size]; + for (int i = 0; i < FillAllIterations; i++) { - Array.Clear(array, 0, length); + TestFillAllArray(a); } } - #endregion - #region GetIterationsFor - private static int[] GetIterationsForConstructor() + static void FillAllReverseSpanBase() { - int[] iterations = new int[8]; - - if (typeof(T) == typeof(string)) - { - iterations[0] = BaseIterations / 10; - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 10; - iterations[7] = BaseIterations / 10; - } - else + byte[] a = new byte[Size]; + Span s = new Span(a); + for (int i = 0; i < FillAllIterations; i++) { - iterations[0] = BaseIterations * 10; - iterations[1] = BaseIterations * 10; - iterations[2] = BaseIterations * 10; - iterations[3] = BaseIterations * 10; - iterations[4] = BaseIterations * 10; - iterations[5] = BaseIterations * 10; - iterations[6] = BaseIterations * 10; - iterations[7] = BaseIterations * 10; + TestFillAllReverseSpan(s); } - - return iterations; } - private static int[] GetIterationsForDangerousGetPinnableReference() - { - int[] iterations = new int[8]; - - iterations[0] = BaseIterations * 10; - iterations[1] = BaseIterations * 10; - iterations[2] = BaseIterations * 10; - iterations[3] = BaseIterations * 10; - iterations[4] = BaseIterations * 10; - iterations[5] = BaseIterations * 10; - iterations[6] = BaseIterations * 10; - iterations[7] = BaseIterations * 10; - - return iterations; - } - - private static int[] GetIterationsForIndex() + static void FillAllReverseArrayBase() { - int[] iterations = new int[8]; - - iterations[0] = BaseIterations; - if (typeof(T) == typeof(string)) - { - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 1000; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; - } - else + byte[] a = new byte[Size]; + for (int i = 0; i < FillAllIterations; i++) { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 100; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; + TestFillAllReverseArray(a); } - - return iterations; } - private static int[] GetIterationsForSlice() - { - int[] iterations = new int[8]; - - iterations[0] = BaseIterations; - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 1000; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; - - return iterations; - } - - private static int[] GetIterationsForToArray() + static void QuickSortSpanBase() { - int[] iterations = new int[8]; + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); - iterations[0] = BaseIterations; - if (typeof(T) == typeof(byte)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 100; - iterations[7] = BaseIterations / 1000000; - } - else if(typeof(T) == typeof(int)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 100; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; - } - else + for (int i = 0; i < QuickSortIterations; i++) { - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 1000; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; + Array.Copy(unsortedData, data, Size); + TestQuickSortSpan(span); } - - return iterations; } - private static int[] GetIterationsForCopyTo() + static void BubbleSortSpanBase() { - int[] iterations = new int[8]; + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); + Span span = new Span(data); - iterations[0] = BaseIterations; - if (typeof(T) == typeof(byte) || (typeof(T) == typeof(int))) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 100; - iterations[7] = BaseIterations / 1000000; - } - else + for (int i = 0; i < BubbleSortIterations; i++) { - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; + Array.Copy(unsortedData, data, Size); + TestBubbleSortSpan(span); } - - return iterations; } - private static int[] GetIterationsForFill() + static void QuickSortArrayBase() { - int[] iterations = new int[8]; + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); - iterations[0] = BaseIterations; - if (typeof(T) == typeof(byte)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 100; - iterations[7] = BaseIterations / 100000; - } - else if (typeof(T) == typeof(string)) - { - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 1000; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; - } - else if (typeof(T) == typeof(TestStruct)) - { - iterations[1] = BaseIterations / 10; - iterations[2] = BaseIterations / 10; - iterations[3] = BaseIterations / 100; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 1000; - iterations[6] = BaseIterations / 10000; - iterations[7] = BaseIterations / 10000000; - } - else + for (int i = 0; i < QuickSortIterations; i++) { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 100; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; + Array.Copy(unsortedData, data, Size); + TestQuickSortArray(data); } - - return iterations; } - private static int[] GetIterationsForClear() + static void BubbleSortArrayBase() { - int[] iterations = new int[8]; + int[] data = new int[Size]; + int[] unsortedData = GetUnsortedData(); - iterations[0] = BaseIterations; - if (typeof(T) == typeof(byte)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 100; - iterations[7] = BaseIterations / 100000; - } - else if (typeof(T) == typeof(string)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 100; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; - } - else if (typeof(T) == typeof(TestStruct)) - { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 100; - iterations[5] = BaseIterations / 100; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; - } - else + for (int i = 0; i < BubbleSortIterations; i++) { - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations / 10; - iterations[4] = BaseIterations / 10; - iterations[5] = BaseIterations / 10; - iterations[6] = BaseIterations / 1000; - iterations[7] = BaseIterations / 1000000; + Array.Copy(unsortedData, data, Size); + TestBubbleSortArray(data); } - - return iterations; - } - - private static int[] GetIterations() - { - int[] iterations = new int[8]; - - iterations[0] = BaseIterations; - iterations[1] = BaseIterations; - iterations[2] = BaseIterations; - iterations[3] = BaseIterations; - iterations[4] = BaseIterations; - iterations[5] = BaseIterations; - iterations[6] = BaseIterations; - iterations[7] = BaseIterations; - - return iterations; } #endregion - #region Helpers - private static T[][] GetArrays() - { - T[][] arrays = new T[8][]; - - arrays[0] = new T[1]; - arrays[1] = new T[5]; - arrays[2] = new T[10]; - arrays[3] = new T[100]; - arrays[4] = new T[500]; - arrays[5] = new T[1000]; - arrays[6] = new T[10000]; - arrays[7] = new T[10000000]; - - return arrays; - } - - private static string[] GetStrings() + static double Bench(Action f) { - string[] strings = new string[8]; - Random rand = new Random(42); - - strings[0] = ""; - strings[1] = ""; - strings[2] = ""; - strings[3] = ""; - strings[4] = ""; - strings[5] = ""; - strings[6] = ""; - strings[7] = ""; - - StringBuilder sb = new StringBuilder(); - char[] c = new char[1]; - for (int i = 0; i < 10000000; i++) - { - c[0] = (char)rand.Next(32, 126); - sb.Append(new string(c)); - if (i == 1-1) - { - strings[0] = sb.ToString(); - } - if (i == 5-1) - { - strings[1] = sb.ToString(); - } - if (i == 10-1) - { - strings[2] = sb.ToString(); - } - if (i == 100-1) - { - strings[3] = sb.ToString(); - } - if (i == 500-1) - { - strings[4] = sb.ToString(); - } - if (i == 1000-1) - { - strings[5] = sb.ToString(); - } - if (i == 10000-1) - { - strings[6] = sb.ToString(); - } - } - strings[7] = sb.ToString(); - - return strings; + Stopwatch sw = Stopwatch.StartNew(); + f(); + sw.Stop(); + return sw.Elapsed.TotalMilliseconds; } - private static TestClass[] GetClasses() + static IEnumerable MakeArgs(params string[] args) { - TestClass[] classes = new TestClass[8]; - - classes[0] = new TestClass(); - classes[1] = new TestClass(); - classes[2] = new TestClass(); - classes[3] = new TestClass(); - classes[4] = new TestClass(); - classes[5] = new TestClass(); - classes[6] = new TestClass(); - classes[7] = new TestClass(); - - classes[0].C0 = new T[1]; - classes[1].C0 = new T[5]; - classes[2].C0 = new T[10]; - classes[3].C0 = new T[100]; - classes[4].C0 = new T[500]; - classes[5].C0 = new T[1000]; - classes[6].C0 = new T[10000]; - classes[7].C0 = new T[10000000]; - - return classes; + return args.Select(arg => new object[] { arg }); } - [StructLayout(LayoutKind.Sequential)] - private sealed class TestClass - { - private double _d; - public T[] C0; - } + static IEnumerable TestFuncs = MakeArgs( + "FillAllSpanBase", "FillAllArrayBase", + "FillAllReverseSpanBase", "FillAllReverseArrayBase", + "BubbleSortSpanBase", "BubbleSortArrayBase", + "QuickSortSpanBase", "QuickSortArrayBase" + ); - [StructLayout(LayoutKind.Sequential)] - private struct TestStruct + static Action LookupFunc(object o) { - public int I; - public string S; + TypeInfo t = typeof(SpanBench).GetTypeInfo(); + MethodInfo m = t.GetDeclaredMethod((string) o); + return m.CreateDelegate(typeof(Action)) as Action; } - #endregion public static int Main(string[] args) { bool result = true; - /*foreach(object[] o in TestFuncs) + foreach(object[] o in TestFuncs) { string funcName = (string) o[0]; Action func = LookupFunc(funcName); double timeInMs = Bench(func); Console.WriteLine("{0}: {1}ms", funcName, timeInMs); - }*/ - -/*#if !DEBUG - TestSpanConstructor(); - TestSpanDangerousCreate(); - TestSpanDangerousGetPinnableReference(); - TestSpanIndex(); - TestSpanSlice(); - TestSpanToArray(); - TestSpanCopyTo(); - TestSpanFill(); - TestSpanClear(); - TestSpanAsBytes(); - TestSpanNonPortableCast(); - TestSpanSliceString(); -#endif*/ - + } + return (result ? 100 : -1); } } From cbc6187795a1f6cb7ceb4d2992d7d658225a9f79 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Sat, 25 Feb 2017 01:59:51 -0800 Subject: [PATCH 3/7] Reducing iteration count so tests finish in < 2 minutes --- tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index bd2df25b9542..2398108ccd9f 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -27,9 +27,9 @@ public class SpanBench const int FillAllIterations = 1; const int BaseIterations = 1; #else - const int BubbleSortIterations = 1000; - const int QuickSortIterations = 10000; - const int FillAllIterations = 1000000; + const int BubbleSortIterations = 100; + const int QuickSortIterations = 100; + const int FillAllIterations = 100; const int BaseIterations = 100; #endif From 8a93c09e30649182d6028460c486056764776cd5 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Wed, 1 Mar 2017 13:48:43 -0800 Subject: [PATCH 4/7] Reducing the number of test cases --- .../Performance/CodeQuality/Span/SpanBench.cs | 159 ------------------ 1 file changed, 159 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index 2398108ccd9f..fa2a71dfbf99 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -366,12 +366,9 @@ public static void BubbleSortArray() #region TestSpanConstructor [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanConstructorByte(int length) { @@ -385,12 +382,9 @@ public static void TestSpanConstructorByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanConstructorInt(int length) { @@ -404,12 +398,9 @@ public static void TestSpanConstructorInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanConstructorString(int length) { @@ -423,12 +414,9 @@ public static void TestSpanConstructorString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanConstructorTestStruct(int length) { @@ -444,12 +432,9 @@ public static void TestSpanConstructorTestStruct(int length) #region TestSpanDangerousCreate [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousCreateByte(int length) { @@ -464,12 +449,9 @@ public static void TestSpanDangerousCreateByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousCreateInt(int length) { @@ -484,12 +466,9 @@ public static void TestSpanDangerousCreateInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousCreateString(int length) { @@ -504,12 +483,9 @@ public static void TestSpanDangerousCreateString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousCreateTestStruct(int length) { @@ -526,12 +502,9 @@ public static void TestSpanDangerousCreateTestStruct(int length) #region TestSpanDangerousGetPinnableReference [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceByte(int length) { @@ -547,12 +520,9 @@ public static void TestSpanDangerousGetPinnableReferenceByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceInt(int length) { @@ -568,12 +538,9 @@ public static void TestSpanDangerousGetPinnableReferenceInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceString(int length) { @@ -589,12 +556,9 @@ public static void TestSpanDangerousGetPinnableReferenceString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) { @@ -612,12 +576,9 @@ public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) #region TestSpanIndex [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanIndexByte(int length) { @@ -633,12 +594,9 @@ public static void TestSpanIndexByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanIndexInt(int length) { @@ -653,12 +611,9 @@ public static void TestSpanIndexInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanIndexString(int length) { @@ -673,12 +628,9 @@ public static void TestSpanIndexString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanIndexTestStruct(int length) { @@ -695,12 +647,9 @@ public static void TestSpanIndexTestStruct(int length) #region TestArrayIndex [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestArrayIndexByte(int length) { @@ -715,12 +664,9 @@ public static void TestArrayIndexByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestArrayIndexInt(int length) { @@ -734,12 +680,9 @@ public static void TestArrayIndexInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestArrayIndexString(int length) { @@ -753,12 +696,9 @@ public static void TestArrayIndexString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestArrayIndexTestStruct(int length) { @@ -774,12 +714,9 @@ public static void TestArrayIndexTestStruct(int length) #region TestSpanSlice [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanSliceByte(int length) { @@ -794,12 +731,9 @@ public static void TestSpanSliceByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanSliceInt(int length) { @@ -814,12 +748,9 @@ public static void TestSpanSliceInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanSliceString(int length) { @@ -834,12 +765,9 @@ public static void TestSpanSliceString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanSliceTestStruct(int length) { @@ -857,12 +785,9 @@ public static void TestSpanSliceTestStruct(int length) #region TestSpanToArray [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanToArrayByte(int length) { @@ -877,12 +802,9 @@ public static void TestSpanToArrayByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanToArrayInt(int length) { @@ -897,12 +819,9 @@ public static void TestSpanToArrayInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanToArrayString(int length) { @@ -917,12 +836,9 @@ public static void TestSpanToArrayString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanToArrayTestStruct(int length) { @@ -939,12 +855,9 @@ public static void TestSpanToArrayTestStruct(int length) #region TestSpanCopyTo [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanCopyToByte(int length) { @@ -960,12 +873,9 @@ public static void TestSpanCopyToByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanCopyToInt(int length) { @@ -981,12 +891,9 @@ public static void TestSpanCopyToInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanCopyToString(int length) { @@ -1002,12 +909,9 @@ public static void TestSpanCopyToString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanCopyToTestStruct(int length) { @@ -1025,12 +929,9 @@ public static void TestSpanCopyToTestStruct(int length) #region TestArrayCopyTo [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayCopyToByte(int length) { @@ -1044,12 +945,9 @@ public static void TestArrayCopyToByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayCopyToInt(int length) { @@ -1063,12 +961,9 @@ public static void TestArrayCopyToInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayCopyToString(int length) { @@ -1082,12 +977,9 @@ public static void TestArrayCopyToString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayCopyToTestStruct(int length) { @@ -1103,12 +995,9 @@ public static void TestArrayCopyToTestStruct(int length) #region TestSpanFill [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanFillByte(int length) { @@ -1122,12 +1011,9 @@ public static void TestSpanFillByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanFillInt(int length) { @@ -1141,12 +1027,9 @@ public static void TestSpanFillInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanFillString(int length) { @@ -1160,12 +1043,9 @@ public static void TestSpanFillString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanFillTestStruct(int length) { @@ -1181,12 +1061,9 @@ public static void TestSpanFillTestStruct(int length) #region TestSpanClear [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanClearByte(int length) { @@ -1200,12 +1077,9 @@ public static void TestSpanClearByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanClearInt(int length) { @@ -1219,12 +1093,9 @@ public static void TestSpanClearInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanClearString(int length) { @@ -1238,12 +1109,9 @@ public static void TestSpanClearString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestSpanClearTestStruct(int length) { @@ -1259,12 +1127,9 @@ public static void TestSpanClearTestStruct(int length) #region TestArrayClear [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayClear(int length) { @@ -1277,12 +1142,9 @@ public static void TestArrayClear(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayClearInt(int length) { @@ -1295,12 +1157,9 @@ public static void TestArrayClearInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayClearString(int length) { @@ -1313,12 +1172,9 @@ public static void TestArrayClearString(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(100000)] public static void TestArrayClearTestStruct(int length) { @@ -1333,12 +1189,9 @@ public static void TestArrayClearTestStruct(int length) #region TestSpanAsBytes [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanAsBytesByte(int length) { @@ -1354,12 +1207,9 @@ public static void TestSpanAsBytesByte(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanAsBytesInt(int length) { @@ -1377,12 +1227,9 @@ public static void TestSpanAsBytesInt(int length) #region TestSpanNonPortableCast [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanNonPortableCastFromByteToInt(int length) { @@ -1398,12 +1245,9 @@ public static void TestSpanNonPortableCastFromByteToInt(int length) [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanNonPortableCastFromIntToByte(int length) { @@ -1421,12 +1265,9 @@ public static void TestSpanNonPortableCastFromIntToByte(int length) #region TestSpanSliceStringChar [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] - [InlineData(5)] [InlineData(10)] [InlineData(100)] - [InlineData(500)] [InlineData(1000)] - [InlineData(10000)] [InlineData(10000000)] public static void TestSpanSliceStringChar(int length) { From 4bb7582b27c7f2b69f2f7b8a74e14adaba415d86 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Wed, 1 Mar 2017 14:46:16 -0800 Subject: [PATCH 5/7] Commenting out ref DangerousGetPinnableReference test --- tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index fa2a71dfbf99..fc4e31a19856 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -500,7 +500,7 @@ public static void TestSpanDangerousCreateTestStruct(int length) #endregion #region TestSpanDangerousGetPinnableReference - [Benchmark(InnerIterationCount = BaseIterations)] + /*[Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] [InlineData(10)] [InlineData(100)] @@ -570,7 +570,7 @@ public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) { ref TestStruct temp = ref span.DangerousGetPinnableReference(); } - } + }*/ #endregion #region TestSpanIndex From 530efaf7444b13e8f4a29589c9233127908c5c73 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Wed, 1 Mar 2017 15:29:31 -0800 Subject: [PATCH 6/7] Updated csproj to use latest compiler --- tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs | 4 ++-- tests/src/JIT/Performance/CodeQuality/Span/SpanBench.csproj | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index fc4e31a19856..fa2a71dfbf99 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -500,7 +500,7 @@ public static void TestSpanDangerousCreateTestStruct(int length) #endregion #region TestSpanDangerousGetPinnableReference - /*[Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(1)] [InlineData(10)] [InlineData(100)] @@ -570,7 +570,7 @@ public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) { ref TestStruct temp = ref span.DangerousGetPinnableReference(); } - }*/ + } #endregion #region TestSpanIndex diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.csproj b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.csproj index e932acf2a263..ba240107ba8d 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.csproj +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.csproj @@ -1,5 +1,7 @@  + + Debug From f98c3ddf9cc02154eb89ca6fb26ab15e827a006a Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Wed, 1 Mar 2017 18:50:29 -0800 Subject: [PATCH 7/7] Tuning test cases and runtime --- .../Performance/CodeQuality/Span/SpanBench.cs | 469 +----------------- 1 file changed, 17 insertions(+), 452 deletions(-) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index fa2a71dfbf99..b0358d226833 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -28,9 +28,9 @@ public class SpanBench const int BaseIterations = 1; #else const int BubbleSortIterations = 100; - const int QuickSortIterations = 100; - const int FillAllIterations = 100; - const int BaseIterations = 100; + const int QuickSortIterations = 1000; + const int FillAllIterations = 100000; + const int BaseIterations = 10000000; #endif const int Size = 1024; @@ -44,13 +44,6 @@ private sealed class TestClass public T[] C0; } - [StructLayout(LayoutKind.Sequential)] - private struct TestStruct - { - public int I; - public string S; - } - [MethodImpl(MethodImplOptions.NoInlining)] static void TestFillAllSpan(Span span) { @@ -369,7 +362,6 @@ public static void BubbleSortArray() [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanConstructorByte(int length) { var array = new byte[length]; @@ -380,28 +372,11 @@ public static void TestSpanConstructorByte(int length) span = new Span(array); } - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanConstructorInt(int length) - { - var array = new int[length]; - Span span; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span = new Span(array); - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanConstructorString(int length) { var array = new string[length]; @@ -411,22 +386,6 @@ public static void TestSpanConstructorString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) span = new Span(array); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanConstructorTestStruct(int length) - { - var array = new TestStruct[length]; - Span span; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span = new Span(array); - } #endregion #region TestSpanDangerousCreate @@ -435,7 +394,6 @@ public static void TestSpanConstructorTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanDangerousCreateByte(int length) { TestClass testClass = new TestClass(); @@ -452,24 +410,6 @@ public static void TestSpanDangerousCreateByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanDangerousCreateInt(int length) - { - TestClass testClass = new TestClass(); - testClass.C0 = new int[length]; - Span span; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanDangerousCreateString(int length) { TestClass testClass = new TestClass(); @@ -480,23 +420,6 @@ public static void TestSpanDangerousCreateString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanDangerousCreateTestStruct(int length) - { - TestClass testClass = new TestClass(); - testClass.C0 = new TestStruct[length]; - Span span; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span = Span.DangerousCreate(testClass, ref testClass.C0[0], testClass.C0.Length); - } #endregion #region TestSpanDangerousGetPinnableReference @@ -505,7 +428,6 @@ public static void TestSpanDangerousCreateTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceByte(int length) { var array = new byte[length]; @@ -523,25 +445,6 @@ public static void TestSpanDangerousGetPinnableReferenceByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanDangerousGetPinnableReferenceInt(int length) - { - var array = new int[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - { - ref int temp = ref span.DangerousGetPinnableReference(); - } - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanDangerousGetPinnableReferenceString(int length) { var array = new string[length]; @@ -553,24 +456,6 @@ public static void TestSpanDangerousGetPinnableReferenceString(int length) ref string temp = ref span.DangerousGetPinnableReference(); } } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - { - ref TestStruct temp = ref span.DangerousGetPinnableReference(); - } - } #endregion #region TestSpanIndex @@ -579,7 +464,6 @@ public static void TestSpanDangerousGetPinnableReferenceTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanIndexByte(int length) { var array = new byte[length]; @@ -597,24 +481,6 @@ public static void TestSpanIndexByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanIndexInt(int length) - { - var array = new int[length]; - var span = new Span(array); - int temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span[length / 2]; - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanIndexString(int length) { var array = new string[length]; @@ -625,23 +491,6 @@ public static void TestSpanIndexString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) temp = span[length / 2]; } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanIndexTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - TestStruct temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span[length / 2]; - } #endregion #region TestArrayIndex @@ -650,7 +499,6 @@ public static void TestSpanIndexTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestArrayIndexByte(int length) { var array = new byte[length]; @@ -667,23 +515,6 @@ public static void TestArrayIndexByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestArrayIndexInt(int length) - { - var array = new int[length]; - int temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = array[length / 2]; - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestArrayIndexString(int length) { var array = new string[length]; @@ -693,22 +524,6 @@ public static void TestArrayIndexString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) temp = array[length / 2]; } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestArrayIndexTestStruct(int length) - { - var array = new TestStruct[length]; - TestStruct temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = array[length / 2]; - } #endregion #region TestSpanSlice @@ -717,7 +532,6 @@ public static void TestArrayIndexTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanSliceByte(int length) { var array = new byte[length]; @@ -734,24 +548,6 @@ public static void TestSpanSliceByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanSliceInt(int length) - { - var array = new int[length]; - var span = new Span(array); - Span temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span.Slice(length / 2); - } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanSliceString(int length) { var array = new string[length]; @@ -762,33 +558,14 @@ public static void TestSpanSliceString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) temp = span.Slice(length / 2); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(10000000)] - public static void TestSpanSliceTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - Span temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span.Slice(length / 2); - - } #endregion #region TestSpanToArray - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanToArrayByte(int length) { var array = new byte[length]; @@ -800,29 +577,11 @@ public static void TestSpanToArrayByte(int length) temp = span.ToArray(); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanToArrayInt(int length) - { - var array = new int[length]; - var span = new Span(array); - int[] temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span.ToArray(); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanToArrayString(int length) { var array = new string[length]; @@ -833,32 +592,14 @@ public static void TestSpanToArrayString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) temp = span.ToArray(); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanToArrayTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - TestStruct[] temp; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - temp = span.ToArray(); - } #endregion #region TestSpanCopyTo - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanCopyToByte(int length) { var array = new byte[length]; @@ -871,30 +612,11 @@ public static void TestSpanCopyToByte(int length) span.CopyTo(destination); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanCopyToInt(int length) - { - var array = new int[length]; - var span = new Span(array); - var destArray = new int[array.Length]; - var destination = new Span(destArray); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.CopyTo(destination); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanCopyToString(int length) { var array = new string[length]; @@ -906,33 +628,14 @@ public static void TestSpanCopyToString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) span.CopyTo(destination); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanCopyToTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - var destArray = new TestStruct[array.Length]; - var destination = new Span(destArray); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.CopyTo(destination); - } #endregion #region TestArrayCopyTo - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestArrayCopyToByte(int length) { var array = new byte[length]; @@ -943,28 +646,11 @@ public static void TestArrayCopyToByte(int length) array.CopyTo(destination, 0); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestArrayCopyToInt(int length) - { - var array = new int[length]; - var destination = new int[array.Length]; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - array.CopyTo(destination, 0); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestArrayCopyToString(int length) { var array = new string[length]; @@ -974,31 +660,14 @@ public static void TestArrayCopyToString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) array.CopyTo(destination, 0); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestArrayCopyToTestStruct(int length) - { - var array = new TestStruct[length]; - var destination = new TestStruct[array.Length]; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - array.CopyTo(destination, 0); - } #endregion #region TestSpanFill - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations * 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanFillByte(int length) { var array = new byte[length]; @@ -1009,28 +678,11 @@ public static void TestSpanFillByte(int length) span.Fill(default(byte)); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanFillInt(int length) - { - var array = new int[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.Fill(default(int)); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 100)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanFillString(int length) { var array = new string[length]; @@ -1040,31 +692,14 @@ public static void TestSpanFillString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) span.Fill(default(string)); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanFillTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.Fill(default(TestStruct)); - } #endregion #region TestSpanClear - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanClearByte(int length) { var array = new byte[length]; @@ -1075,28 +710,11 @@ public static void TestSpanClearByte(int length) span.Clear(); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanClearInt(int length) - { - var array = new int[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.Clear(); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestSpanClearString(int length) { var array = new string[length]; @@ -1106,32 +724,15 @@ public static void TestSpanClearString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) span.Clear(); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestSpanClearTestStruct(int length) - { - var array = new TestStruct[length]; - var span = new Span(array); - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - span.Clear(); - } #endregion #region TestArrayClear - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] - public static void TestArrayClear(int length) + public static void TestArrayClearByte(int length) { var array = new byte[length]; foreach (var iteration in Benchmark.Iterations) @@ -1140,27 +741,11 @@ public static void TestArrayClear(int length) Array.Clear(array, 0, length); } - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestArrayClearInt(int length) - { - var array = new int[length]; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - Array.Clear(array, 0, length); - } - - [Benchmark(InnerIterationCount = BaseIterations)] + [Benchmark(InnerIterationCount = BaseIterations / 10)] [InlineData(1)] [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(100000)] public static void TestArrayClearString(int length) { var array = new string[length]; @@ -1169,21 +754,6 @@ public static void TestArrayClearString(int length) for (int i = 0; i < Benchmark.InnerIterationCount; i++) Array.Clear(array, 0, length); } - - [Benchmark(InnerIterationCount = BaseIterations)] - [InlineData(1)] - [InlineData(10)] - [InlineData(100)] - [InlineData(1000)] - [InlineData(100000)] - public static void TestArrayClearTestStruct(int length) - { - var array = new TestStruct[length]; - foreach (var iteration in Benchmark.Iterations) - using (iteration.StartMeasurement()) - for (int i = 0; i < Benchmark.InnerIterationCount; i++) - Array.Clear(array, 0, length); - } #endregion #region TestSpanAsBytes @@ -1192,7 +762,6 @@ public static void TestArrayClearTestStruct(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanAsBytesByte(int length) { var array = new byte[length]; @@ -1210,7 +779,6 @@ public static void TestSpanAsBytesByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanAsBytesInt(int length) { var array = new int[length]; @@ -1230,7 +798,6 @@ public static void TestSpanAsBytesInt(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanNonPortableCastFromByteToInt(int length) { var array = new byte[length]; @@ -1248,7 +815,6 @@ public static void TestSpanNonPortableCastFromByteToInt(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanNonPortableCastFromIntToByte(int length) { var array = new int[length]; @@ -1268,7 +834,6 @@ public static void TestSpanNonPortableCastFromIntToByte(int length) [InlineData(10)] [InlineData(100)] [InlineData(1000)] - [InlineData(10000000)] public static void TestSpanSliceStringChar(int length) { StringBuilder sb = new StringBuilder();