I have two clarifications to add to @Marc Gravell answer.
Tests were done in .NET 6 in x64 release.
Test code is at end.
Array and List not tested in same way
To test array and List under same condition, "for" should be modified as well.
for (int i = 0; i < arr.Length; i++)
New version :
int len = arr.Length;
for (int i = 0; i < len; i++)
Bottleneck List/foreach :
The bottleneck with List (List/foreach test) can be fixed.
Change it to :
list.ForEach(x => chk += x);
Test run on Laptop on Windows 10 pro 21H1 x64 with Core i7-10510U
List/for Count out: 1495ms (589725196)
List/for Count in: 1706ms (589725196)
Array/for Count out: 945ms (589725196)
Array/for Count in: 1072ms (589725196)
List/foreach: 2114ms (589725196)
List/foreach fixed: 1210ms (589725196)
Array/foreach: 1179ms (589725196)
Results interpretation
Array/for is faster than original test. (12% less)
List/foreach fixed is faster than List/for.
List/foreach fixed is close to Array/foreach.
I have run this test several times. Results change but orders of magnitude remain the same.
These results of this test show that you really have to have great need for performance to be forced to use Array.
Depending on method used to manipulate List, performance can be divided by 2.
This test is partial. There is no random access, direct access, write access test, etc.
Did I get some parts wrong or do you have any other ideas for improving performance?
Test code :
using System;
using System.Collections.Generic;
using System.Diagnostics;
static class Program
{
static void Main()
{ List<int> list = new List<int>(6000000);
Random rand = new Random(12345);
for (int i = 0; i < 6000000; i++)
{
list.Add(rand.Next(5000));
}
int[] arr = list.ToArray();
int chk = 0;
Stopwatch watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
int len = list.Count;
for (int i = 0; i < len; i++)
{
chk += list[i];
}
}
watch.Stop();
Console.WriteLine("List/for Count out: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
Stopwatch watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
for (int i = 0; i < list.Count; i++)
{
chk += list[i];
}
}
watch.Stop();
Console.WriteLine("List/for Count in: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
int len = arr.Length;
for (int i = 0; i < len; i++)
{
chk += arr[i];
}
}
watch.Stop();
Console.WriteLine("Array/for Count out: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
for (int i = 0; i < arr.Length; i++)
{
chk += arr[i];
}
}
watch.Stop();
Console.WriteLine("Array/for Count in: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
foreach (int i in list)
{
chk += i;
}
}
watch.Stop();
Console.WriteLine("List/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
list.ForEach(i => chk += i);
}
watch.Stop();
Console.WriteLine("List/foreach fixed: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
chk = 0;
watch = Stopwatch.StartNew();
for (int rpt = 0; rpt < 100; rpt++)
{
foreach (int i in arr)
{
chk += i;
}
}
watch.Stop();
Console.WriteLine("Array/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk);
Console.ReadLine();
}
}