Geek Culture
Published in

Geek Culture

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

Image Source: og_cpu_benchmarks.png

Hi, today we will talk about Dynamic List<T>, size-specific List<T>(10), and Array List of Benchmark test.

Firstly we will create a .Net 6.0 Console Application and we will write Benchmark for Testing List<T>, Array, and List<T>(count).

Add the “BenchmarkDotNet” package to the project.

dotnet add package BenchmarkDotNet --version 0.13.1

In the first scenario, we will create two List<>, one of them is a dynamic list and, in the second one we will set capacity at the constructor of the list. We will add different count numbers many times to both of the List and check the Benchmark result.

We will create a “ListCapacityPerformance” class and mark it with the [MemoryDiagnoser] attribute. MemoryDiagnoser allows measuring the number of allocated bytes and garbage collection frequency.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
[MemoryDiagnoser]
public class ListCapacityPerformance
{
.
.
}

With [Params()] attribute, you can specify a set of values. Every value must be a compile-time constant. As a result, you will get results for each combination of params values.

[Params(20, 80, 300, 800)]
public int capacity;

In the below example, we will create List<> but not set any capacity. After all, we will add numbers for every variable of the “count” array to this dynamic List. In the end, we will check memory allocation, total time, etc.

[Benchmark]
public List<int> DynamicCapacity()
{
List<int> squidList = new List<int>();
for (int i = 0; i < capacity; i++)
{
squidList.Add(i);
}
return squidList;
}

In the second example, we will create “List<int>(capacity)” with static capacity. For every loop, we set the list length as capacity value.

[Benchmark]
public List<int> SetCapacity()
{
List<int> squidList = new List<int>(capacity);
for (int i = 0; i < capacity; i++)
{
squidList.Add(i);
}
return squidList;
}

Build-in” Release” Mode. And “Start Without Debugging”. Below, you can see the Static capacity list is less allocated and faster. With Larger values, the gap becomes even wider.

If you add an item to List<>(x), more than its capacity, C# extends List’s size, 2 * times the current size, for every overload. List<> always checks its limit before every item is added.

There is one handicap on the List<>. In the below example, we set the capacity of List as a 4. If we added the item to the list more than 4 it raised its capacity twice automatically at every overload.

Let’s check the below example: It is .Net 6 Console application without Main() method :)

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);

If you see the result, at the beginning the List capacity is 4 and its count is 0. After we added 4 items, the List capacity was again 4 but its count this time was 4 too. After we added 2 items and overload the List of lengths, its capacity was raised and become 8(2*4) and its count was 6. In the end, when we added 4 more items, the List<> count was 10, and capacity was raised and become 16 (2*8).

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

Sometimes against lots of advantages of Lists, working with arrays could be better.

Let’s check the below example: Like the upper example, we added from 1 to max count number to a List and an Array. We did the same thing for each combination of the count array.

As we can see in the benchmarks the array version is much faster. I know most of the developers prefer List against the Array. Why?

Because :

  • List has special methods for adding, removing, searching, and sorting elements.
  • The List<T>is a dynamic data structure, meaning we do not need to know the number of elements when we declare it. So List size can be scaled up on run time.

Arrays are less useful than List but much faster and you could search the elements by index on Array. But List requires iteration, this means a slow search time for the List. Arrays require less memory, unlike the Lists. But Lists have lots of useful special methods so they are more preferable than arrays.

Conclusion:

There are many different data structures. Each data structure has strengths and weaknesses which affect performance depending on the task. Today we talked about Arrays and Lists. I hope this article helped you to understand array and list differences. Happy New Year. See you later until the next article in 2022. Bye

“If you have read so far, first of all, thank you for your patience and support. I welcome all of you to my blog for more!”

Source:

--

--

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

Bora Kaşmer

2.1K Followers

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