diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2019-02-28 05:05:59 +0300 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2019-02-28 05:05:59 +0300 |
commit | 518f26af367ae05ed2db4a83cda5d51b8d5ce39f (patch) | |
tree | 9f3b4e4fab93a64fae1124a54abad32f09dde54e | |
parent | 8be3bf9237fd7a294b350cb22198b8e354dc3054 (diff) |
Add benchmarks for array, span, and string pinningpinning_benchmarks
-rw-r--r-- | sandbox/PerfBenchmarkDotNet/PerfBenchmarkDotNet.csproj | 1 | ||||
-rw-r--r-- | sandbox/PerfBenchmarkDotNet/Program.cs | 1 | ||||
-rw-r--r-- | sandbox/PerfBenchmarkDotNet/SpanBenchmarks.cs | 72 |
3 files changed, 74 insertions, 0 deletions
diff --git a/sandbox/PerfBenchmarkDotNet/PerfBenchmarkDotNet.csproj b/sandbox/PerfBenchmarkDotNet/PerfBenchmarkDotNet.csproj index 1ec7105e..a727c4c1 100644 --- a/sandbox/PerfBenchmarkDotNet/PerfBenchmarkDotNet.csproj +++ b/sandbox/PerfBenchmarkDotNet/PerfBenchmarkDotNet.csproj @@ -2,6 +2,7 @@ <PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworks>net47;netcoreapp2.0</TargetFrameworks> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> </PropertyGroup> <ItemGroup> diff --git a/sandbox/PerfBenchmarkDotNet/Program.cs b/sandbox/PerfBenchmarkDotNet/Program.cs index f611fc6a..b51d63fe 100644 --- a/sandbox/PerfBenchmarkDotNet/Program.cs +++ b/sandbox/PerfBenchmarkDotNet/Program.cs @@ -53,6 +53,7 @@ namespace PerfBenchmarkDotNet typeof(ImproveStringKeySerializeBenchmark), typeof(MessagePackReaderBenchmark), typeof(MessagePackWriterBenchmark), + typeof(SpanBenchmarks), }); // args = new[] { "0" }; diff --git a/sandbox/PerfBenchmarkDotNet/SpanBenchmarks.cs b/sandbox/PerfBenchmarkDotNet/SpanBenchmarks.cs new file mode 100644 index 00000000..4e104036 --- /dev/null +++ b/sandbox/PerfBenchmarkDotNet/SpanBenchmarks.cs @@ -0,0 +1,72 @@ +using System; +using BenchmarkDotNet.Attributes; + +namespace PerfBenchmarkDotNet +{ + public class SpanBenchmarks + { + private const string SomeString = "Hi there"; + private static readonly byte[] byteArray = new byte[1]; + + [Benchmark] + public unsafe void PinString() + { + fixed (char* pChars = SomeString) + { + char ch = pChars[0]; + } + } + + [Benchmark] + public unsafe void GetSpanFromString() + { + char ch = SomeString.AsSpan()[0]; + } + + [Benchmark] + public unsafe void PinArray() + { + fixed (byte* pBytes = byteArray) + { + pBytes[0] = 7; + } + } + + [Benchmark] + public unsafe void PinArray_Indexer() + { + fixed (byte* pBytes = &byteArray[0]) + { + pBytes[0] = 7; + } + } + + [Benchmark] + public void PinSpan() + { + PinSpan_Helper(byteArray); + } + + private static unsafe void PinSpan_Helper(Span<byte> bytes) + { + fixed (byte* pBytes = bytes) + { + pBytes[0] = 7; + } + } + + [Benchmark] + public void PinSpan_Indexer() + { + PinSpan_Indexer_Helper(byteArray); + } + + private static unsafe void PinSpan_Indexer_Helper(Span<byte> bytes) + { + fixed (byte* pBytes = &bytes[0]) + { + pBytes[0] = 8; + } + } + } +} |