Skip to content

Instantly share code, notes, and snippets.

@bellons91
Created July 9, 2023 13:42
Show Gist options
  • Save bellons91/15959f75b98191dd55d2759322501b2f to your computer and use it in GitHub Desktop.
Save bellons91/15959f75b98191dd55d2759322501b2f to your computer and use it in GitHub Desktop.
Hashset .Count vs .Count() benchmark
#LINQPad optimize+
void Main()
{
var summary = BenchmarkRunner.Run<HashSetPerformance>();
}
public class HashSetPerformance
{
private HashSet<int> set;
[Params(2, 100)]
public int SetSize;
[GlobalSetup]
public void GlobalSetup()
{
var items = Enumerable.Range(0, SetSize);
set = new HashSet<int>(items); // executed once per each N value
}
[Benchmark]
public int CountAsProperty() => set.Count;
[Benchmark]
public int CountAsMethod() => set.Count();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment