Skip to content

Instantly share code, notes, and snippets.

@bellons91
Created July 9, 2023 13:43
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 bellons91/3072654de97861898ad756254463601e to your computer and use it in GitHub Desktop.
Save bellons91/3072654de97861898ad756254463601e to your computer and use it in GitHub Desktop.
List with resize Benchmark
#LINQPad optimize+
void Main()
{
var summary = BenchmarkRunner.Run<ListCapacityPerformance>();
}
[MemoryDiagnoser]
public class ListCapacityPerformance
{
[Params(2, 100, 1000, 10000, 100_000)]
public int Size;
[Benchmark]
public void SizeDefined()
{
int itemsCount = Size;
List<int> set = new List<int>(itemsCount);
foreach (var i in Enumerable.Range(0, itemsCount))
{
set.Add(i);
}
}
[Benchmark]
public void SizeNotDefined()
{
int itemsCount = Size;
List<int> set = new List<int>();
foreach (var i in Enumerable.Range(0, itemsCount))
{
set.Add(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment