Created
July 9, 2023 13:43
-
-
Save bellons91/3072654de97861898ad756254463601e to your computer and use it in GitHub Desktop.
List with resize 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<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