Skip to content

Instantly share code, notes, and snippets.

@smoogipoo
Last active November 11, 2019 02:10
Show Gist options
  • Select an option

  • Save smoogipoo/841baef4e8d9f28b877ff41933fadda3 to your computer and use it in GitHub Desktop.

Select an option

Save smoogipoo/841baef4e8d9f28b877ff41933fadda3 to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace Benchmarks
{
class Program
{
static void Main(string[] args)
=> BenchmarkRunner.Run<BenchmarkClass>();
}
public class BenchmarkClass
{
[Benchmark]
public int BenchmarkValue() => runMethod1(1000);
[Benchmark]
public int BenchmarkIsInst() => runMethod2(1000);
private int runMethod1(int? parameter)
{
// Using isinst to cause branching but not cause additional overheads due to method invocation
// We only care about the .Value invocations below
// NB: Each invocation of this method has _two_ .Value invocations
if (parameter is { } value)
{
int result = 0;
for (int i = 0; i < parameter.Value; i++)
result += parameter.Value;
return result;
}
return 0;
}
private int runMethod2(int? parameter)
{
if (parameter is { } value)
{
int result = 0;
for (int i = 0; i < value; i++)
result += value;
return result;
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment