Skip to content

Instantly share code, notes, and snippets.

@zpqrtbnk
Last active November 21, 2020 11:05
Show Gist options
  • Save zpqrtbnk/490025ea4b720ba2f06ae827c88f18ee to your computer and use it in GitHub Desktop.
Save zpqrtbnk/490025ea4b720ba2f06ae827c88f18ee to your computer and use it in GitHub Desktop.
// executed with:
// var config = DefaultConfig.Instance
.WithArtifactsPath(artPath)
.AddDiagnoser(MemoryDiagnoser.Default)
.AddJob(Job.InProcess);
// BenchmarkRunner.Run(typeof(NullCoalesce), config);
public class NullCoalesce
{
private bool _toggle;
public Func<int> F { get; set; }
public NullCoalesce()
{
// F returns a const
//F = () => 0;
// F returns a value
//F = () =>
//{
// _toggle = !_toggle;
// return _toggle ? 1 : 0;
//};
}
[Benchmark]
public void BencharkM1()
{
var i = GetConditional();
}
private int M1()
=> F == null ? 0 : F();
[Benchmark]
public void BenchmarkM2()
{
var i = GetCoalesce();
}
private int M2()
=> F?.Invoke() ?? 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment