Instantly share code, notes, and snippets.

Embed
What would you like to do?
int?のベンチマーク
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=3328124 Hz, Resolution=300.4696 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 x Mean Error StdDev Min Max Gen 0 Allocated
HasValue 1 1.065 ns 0.0716 ns 0.0670 ns 0.9574 ns 1.150 ns - 0 B
IsInt 1 52.917 ns 0.3573 ns 0.2984 ns 52.5679 ns 53.499 ns 0.0057 24 B
IsIntY 1 1.184 ns 0.0492 ns 0.0460 ns 1.1125 ns 1.273 ns - 0 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Columns;
using BenchmarkDotNet.Attributes.Jobs;
using System.Collections.Generic;
namespace Bench
{
[CoreJob]
[MeanColumn, MinColumn, MaxColumn]
[MemoryDiagnoser]
public class NullableBench
{
public IEnumerable<object> Source()
{
yield return (int?)1;
}
[Benchmark]
[ArgumentsSource(nameof(Source))]
public bool HasValue(int? x)
{
if (x.HasValue)
{
return true;
}
return false;
}
[Benchmark]
[ArgumentsSource(nameof(Source))]
public bool IsInt(int? x)
{
if (x is int)
{
return true;
}
return false;
}
[Benchmark]
[ArgumentsSource(nameof(Source))]
public bool IsIntY(int? x)
{
if (x is int y)
{
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment