Skip to content

Commit

Permalink
add GetPinnableReference benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Aug 1, 2018
1 parent 24ecd7f commit 68587cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/benchmarks/corefx/System.Memory/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Benchmarks;
using Consumer = BenchmarkDotNet.Engines.Consumer;

namespace System.Memory
{
[BenchmarkCategory(Categories.CoreCLR, Categories.CoreFX, Categories.Span)]
public class ReadOnlySpan
{
private readonly string _sampeString = "this is a very nice sample string";
private readonly Consumer _consumer = new Consumer();

[Benchmark]
public ReadOnlySpan<char> StringAsSpan() => _sampeString.AsSpan();

[Benchmark(OperationsPerInvoke = 16)]
public void GetPinnableReference()
{
ReadOnlySpan<char> span = _sampeString.AsSpan();
var consumer = _consumer;

consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
}

[Benchmark(OperationsPerInvoke = 16)]
[ArgumentsSource(nameof(IndexOfStringArguments))]
public int IndexOfString(string input, string value, StringComparison comparisonType)
Expand Down
15 changes: 15 additions & 0 deletions src/benchmarks/corefx/System.Memory/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections;
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using Benchmarks;

Expand Down Expand Up @@ -80,5 +81,19 @@ public void SetupBinarySearch()

[Benchmark]
public int BinarySearch() => new System.Span<T>(_emptyWithSingleValue).BinarySearch(_notDefaultValue);

[Benchmark(OperationsPerInvoke = 16)]
public void GetPinnableReference()
{
var span = new System.Span<T>(_array);

Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference());
Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference());
Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference());
Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference()); Consume(span.GetPinnableReference());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void Consume(in T _) { }
}
}

0 comments on commit 68587cd

Please sign in to comment.