Geek Culture
Published in

Geek Culture

List<T> & List<T>(Capacity) & Array Benchmark

Image Source: og_cpu_benchmarks.png
dotnet add package BenchmarkDotNet --version 0.13.1
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
[MemoryDiagnoser]
public class ListCapacityPerformance
{
.
.
}
[Params(20, 80, 300, 800)]
public int capacity;
[Benchmark]
public List<int> DynamicCapacity()
{
List<int> squidList = new List<int>();
for (int i = 0; i < capacity; i++)
{
squidList.Add(i);
}
return squidList;
}
[Benchmark]
public List<int> SetCapacity()
{
List<int> squidList = new List<int>(capacity);
for (int i = 0; i < capacity; i++)
{
squidList.Add(i);
}
return squidList;
}
List<int> squidlist = new List<int>(4);Console.WriteLine("Capacity Is: " + squidlist.Capacity);
Console.WriteLine("Count Is: " + squidlist.Count);
squidlist.Add(1);
squidlist.Add(2);
squidlist.Add(3);
squidlist.Add(4);
Console.WriteLine("Capacity Is: " + squidlist.Capacity);
Console.WriteLine("Count Is: " + squidlist.Count);
squidlist.Add(5);
squidlist.Add(6);
Console.WriteLine("Capacity Is: " + squidlist.Capacity);
Console.WriteLine("Count Is: " + squidlist.Count);
squidlist.Add(7);
squidlist.Add(8);
squidlist.Add(5);
squidlist.Add(9);
Console.WriteLine("Capacity Is: " + squidlist.Capacity);
Console.WriteLine("Count Is: " + squidlist.Count);

There is a cost to exceeding the limit on the List. For this reason, great care should be taken when working with lists, especially in large data.

List vs Array

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bora Kaşmer

I have been coding since 1993. I am computer and civil engineer. Microsoft MVP. Senior Software Architect. Ride motorcycle. Gamer. Have two daughters.