Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Try VS Tuple
BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328129 Hz, Resolution=300.4691 ns, Timer=TSC
.NET Core SDK=2.1.403
  [Host] : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT
  Core   : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT

Job=Core  Runtime=Core  
Method Mean Error StdDev Min Max Gen 0 Allocated
BenchmarkIsInt 9.446 ns 0.1701 ns 0.1591 ns 9.225 ns 9.732 ns 0.0057 24 B
BenchmarkTryIsInt 5.018 ns 0.0881 ns 0.0824 ns 4.887 ns 5.120 ns 0.0057 24 B
BenchmarkIsValue16 11.518 ns 0.0833 ns 0.0602 ns 11.428 ns 11.612 ns 0.0076 32 B
BenchmarkTryIsValue16 6.912 ns 0.0823 ns 0.0687 ns 6.805 ns 7.045 ns 0.0076 32 B
BenchmarkIsValue32 31.532 ns 0.1999 ns 0.1561 ns 31.339 ns 31.822 ns 0.0114 48 B
BenchmarkTryIsValue32 22.509 ns 0.2927 ns 0.2738 ns 21.967 ns 23.019 ns 0.0114 48 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Columns;
using BenchmarkDotNet.Attributes.Jobs;
using System.Runtime.CompilerServices;
namespace Bench
{
[CoreJob]
[MeanColumn, MinColumn, MaxColumn]
[MemoryDiagnoser]
public class TryVsTuple
{
public struct Value16
{
public long X { get; set; }
public long Y { get; set; }
}
public struct Value32
{
public long A { get; set; }
public long B { get; set; }
public long C { get; set; }
public long D { get; set; }
}
[MethodImpl(MethodImplOptions.NoInlining)]
public (bool, int) IsInt(object value)
{
if (value is int x)
{
return (true, x);
}
return (false, default);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public bool TryIsInt(object value, out int result)
{
if (value is int x)
{
result = x;
return true;
}
result = default;
return false;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public (bool, Value16) IsValue16(object value)
{
if (value is Value16 x)
{
return (true, x);
}
return (false, default);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public bool TryIsValue16(object value, out Value16 result)
{
if (value is Value16 x)
{
result = x;
return true;
}
result = default;
return false;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public (bool, Value32) IsValue32(object value)
{
if (value is Value32 x)
{
return (true, x);
}
return (false, default);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public bool TryIsValue32(object value, out Value32 result)
{
if (value is Value32 x)
{
result = x;
return true;
}
result = default;
return false;
}
[Benchmark]
public int BenchmarkIsInt()
{
(bool hasResult, int result) = IsInt(default(int));
if (hasResult)
{
return result;
}
return default;
}
[Benchmark]
public int BenchmarkTryIsInt()
{
bool hasResult = TryIsInt(default(int), out int result);
if (hasResult)
{
return result;
}
return default;
}
[Benchmark]
public Value16 BenchmarkIsValue16()
{
(bool hasResult, Value16 result) = IsValue16(default(Value16));
if (hasResult)
{
return result;
}
return default;
}
[Benchmark]
public Value16 BenchmarkTryIsValue16()
{
bool hasResult = TryIsValue16(default(Value16), out Value16 result);
if (hasResult)
{
return result;
}
return default;
}
[Benchmark]
public Value32 BenchmarkIsValue32()
{
(bool hasResult, Value32 result) = IsValue32(default(Value32));
if (hasResult)
{
return result;
}
return default;
}
[Benchmark]
public Value32 BenchmarkTryIsValue32()
{
bool hasResult = TryIsValue32(default(Value32), out Value32 result);
if (hasResult)
{
return result;
}
return default;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.