Created
May 14, 2019 01:10
-
-
Save HaloFour/8b435c4adc7a7832ac02e4c306131d95 to your computer and use it in GitHub Desktop.
BenchmarkVTPattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
namespace BenchmarkVTPattern | |
{ | |
[CoreJob] | |
[MemoryDiagnoser] | |
public class Benchmark | |
{ | |
private object t1 = ValueTuple.Create(50, 50); | |
private object t2 = ValueTuple.Create<object, object>(50, 50); | |
[Benchmark] | |
public int MatchArityOnly() | |
{ | |
if (t1 is (_, _)) | |
{ | |
return 1; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchValueTuple() | |
{ | |
if (t1 is ValueTuple<int, int> tuple) | |
{ | |
return tuple.Item1 + tuple.Item2; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchValueTupleAndDeconstruct() | |
{ | |
if (t1 is ValueTuple<int, int>(int x, int y)) | |
{ | |
return x + y; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchITuple() | |
{ | |
if (t1 is (int x, int y)) | |
{ | |
return x + y; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchITuplePartially() | |
{ | |
if (t1 is (int x, _)) | |
{ | |
return x; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchITupleInexact() | |
{ | |
if (t2 is (int x, int y)) | |
{ | |
return x + y; | |
} | |
return 0; | |
} | |
[Benchmark] | |
public int MatchValueTupleWithITupleFallback() | |
{ | |
if (t2 is ValueTuple<int, int>(int x, int y)) | |
{ | |
return x + y; | |
} | |
else if (t2 is (int x2, int y2)) | |
{ | |
return x2 + y2; | |
} | |
return 0; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run<Benchmark>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment