diff options
Diffstat (limited to 'src/System.Memory/tests/Performance')
-rw-r--r-- | src/System.Memory/tests/Performance/Perf.Memory.Slice.cs (renamed from src/System.Memory/tests/Performance/Perf.MemorySlice.cs) | 0 | ||||
-rw-r--r-- | src/System.Memory/tests/Performance/Perf.Memory.Span.cs | 166 | ||||
-rw-r--r-- | src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj | 3 |
3 files changed, 168 insertions, 1 deletions
diff --git a/src/System.Memory/tests/Performance/Perf.MemorySlice.cs b/src/System.Memory/tests/Performance/Perf.Memory.Slice.cs index 27cecdef49..27cecdef49 100644 --- a/src/System.Memory/tests/Performance/Perf.MemorySlice.cs +++ b/src/System.Memory/tests/Performance/Perf.Memory.Slice.cs diff --git a/src/System.Memory/tests/Performance/Perf.Memory.Span.cs b/src/System.Memory/tests/Performance/Perf.Memory.Span.cs new file mode 100644 index 0000000000..f2db92e0f4 --- /dev/null +++ b/src/System.Memory/tests/Performance/Perf.Memory.Span.cs @@ -0,0 +1,166 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Buffers; +using System.MemoryTests; +using Microsoft.Xunit.Performance; +using Xunit; + +namespace System.Memory.Tests +{ + public class Perf_Memory_Span + { + private const int InnerCount = 1_000_000; + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromDefaultIntegerMemory() + { + Memory<int> memory = default; + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<int> span = memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromIntegerArrayBackedMemory() + { + int[] a = { 91, 92, -93, 94 }; + var memory = new Memory<int>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<int> span = memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromCharArrayBackedMemory() + { + char[] a = "9192-9394".ToCharArray(); + var memory = new Memory<char>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<char> span = memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromObjectArrayBackedMemory() + { + object o1 = new object(); + object o2 = new object(); + object[] a = { o1, o2 }; + var memory = new Memory<object>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<object> span = memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromStringBackedMemory() + { + string a = "9192-9394"; + ReadOnlyMemory<char> memory = a.AsMemory(); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + ReadOnlySpan<char> span = memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromIntegerMemoryManager() + { + int[] a = { 91, 92, -93, 94 }; + var memory = new Memory<int>(a); + MemoryManager<int> manager = new CustomMemoryForTest<int>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<int> span = manager.Memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromCharMemoryManager() + { + char[] a = "9192-9394".ToCharArray(); + var memory = new Memory<char>(a); + MemoryManager<char> manager = new CustomMemoryForTest<char>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<char> span = manager.Memory.Span; + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + public static void SpanFromObjectMemoryManager() + { + object o1 = new object(); + object o2 = new object(); + object[] a = { o1, o2 }; + var memory = new Memory<object>(a); + MemoryManager<object> manager = new CustomMemoryForTest<object>(a); + + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Benchmark.InnerIterationCount; i++) + { + Span<object> span = manager.Memory.Span; + } + } + } + } + } +} diff --git a/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj b/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj index edb6ffe73c..28249af672 100644 --- a/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj +++ b/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj @@ -10,6 +10,8 @@ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" /> <ItemGroup> <Compile Include="Perf.Base64EncodeDecode.cs" /> + <Compile Include="Perf.Memory.Slice.cs" /> + <Compile Include="Perf.Memory.Span.cs" /> <Compile Include="Perf.ReadOnlySequence.Enumerator.cs" /> <Compile Include="Perf.ReadOnlySequence.First.cs" /> <Compile Include="Perf.ReadOnlySequence.GetPosition.cs" /> @@ -23,7 +25,6 @@ <Compile Include="Perf.Span.IndexOfAny.cs" /> <Compile Include="Perf.Span.SequenceCompareTo.cs" /> <Compile Include="Perf.Span.StartsWith.cs" /> - <Compile Include="Perf.MemorySlice.cs" /> <Compile Include="Perf.Utf8Formatter.cs" /> <Compile Include="Perf.Utf8Parser.cs" /> <Compile Include="..\Base64\Base64TestHelper.cs" /> |