Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Created November 19, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeilCli/5761bf81bc434e9045cad5841601398d to your computer and use it in GitHub Desktop.
Save MeilCli/5761bf81bc434e9045cad5841601398d to your computer and use it in GitHub Desktop.
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