Created
July 9, 2023 13:42
-
-
Save bellons91/15959f75b98191dd55d2759322501b2f to your computer and use it in GitHub Desktop.
Hashset .Count vs .Count() benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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