-1
LIST FOR      00:00:00.0000980
LIST FOREACH  00:00:00.0000007
ARRAY FOR     00:00:00.0028450
ARRAY FOREACH 00:00:00.0051233

I have always made performance heavy things with arrays, but it seems lists are much faster.

using System;
using System.Diagnostics;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var cron = NCrontab.Advanced.CrontabSchedule.Parse("0 0 1 1 * 2016",
            NCrontab.Advanced.Enumerations.CronStringFormat.WithYears);
        var date = new System.DateTime(year: 2016, month: 1, day: 1,
            hour: 0, minute: 0, second: 0);
        
        int[] testArray = new int[1000000];
        List<int> testList = new List<int>(1000000);
        
        var stopWatch = new Stopwatch();
        stopWatch.Start();
        for (int i = 0; i < testList.Count;i++)
        {
            var s = 1;
        }
        stopWatch.Stop();
        Console.WriteLine("LIST FOR      " + stopWatch.Elapsed);
        
        stopWatch = new Stopwatch();
        stopWatch.Start();
        foreach (int i in testList)
        {
            var d = 1;
        }
        stopWatch.Stop();
        Console.WriteLine("LIST FOREACH  " + stopWatch.Elapsed);
                
        stopWatch = new Stopwatch();
        stopWatch.Start();
        for (int i = 0; i < testArray.Length;i++)
        {
            var f = 1;
        }
        stopWatch.Stop();
        Console.WriteLine("ARRAY FOR     " + stopWatch.Elapsed);
        
        stopWatch = new Stopwatch();
        stopWatch.Start();
        foreach (int i in testArray)
        {
            var h = 1;
        }
        stopWatch.Stop();
        Console.WriteLine("ARRAY FOREACH " + stopWatch.Elapsed);
    }
}

https://dotnetfiddle.net/RKWBJl

List internally implements T[], just that its flexible and internally keeps expanding when Capacity is over. Since for the all the important operations, it internally does the array operations, just that it provides the convenience for the dynamic expansion.

I don't understand it, but I need to know for heavy and frequent operations.

Basically I'm trying to determine what's better to have high FPS.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Charlini
  • 35
  • 5
  • 5
    In your test you are not reading or writing value from array or list, you are checking only the count/length and assigning doing a operation which does not involve list/array. The count of list is always 0 as no items are added into list. I've taken your code and change the logic. Check this [fiddle](https://dotnetfiddle.net/Czn49R). You will see that in real example array is faster than list. You can also refer to [https://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists](https://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists) – user1672994 Jan 03 '21 at 10:29
  • 1
    Not a *performance issue as indicated by the duplicate that is not relevant and misleading*. But **The problem is not reproducible because it does not exist** as commented by @.user1672994 and answered by @.KarlJohanSjögren. –  Jan 03 '21 at 11:09
  • thanks buddy @user1672994 your comment and fiddle were of much help – Charlini Jan 03 '21 at 11:58
  • 1
    @OlivierRogier I voted for the question to be reopened, because it doesn't seem to be a duplicate of the [linked question](https://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists). IMHO the question should be closed for the reason "Not reproducible or was caused by a typo", because the reported difference in performance is just the result of flawed measurements. – Theodor Zoulias Jan 03 '21 at 14:46
  • 1
    Charlini the statement `new List(1000000)` creates a list with a [`Capacity`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.capacity) of 1,000,000, not with a `Count` of 1,000,000. You are enumerating an empty list. – Theodor Zoulias Jan 03 '21 at 14:49

1 Answers1

5

Those tests are not comparable.

This creates an array with a million entries.

int[] testArray = new int[1000000];

This creates an empty list, but with memory allocated in the background for up to a million entries (so it doesn't have to resize its internal buffer until it reaches a million.

List<int> testList = new List<int>(1000000);

So testArray.Length will be 1000000 here and testList.Count will be 0 (since you are not adding a million items to it).

Performance wise it will also be a lot of other things to consider then just looping over the values. Adding or removing items can be very expensive for an array if it leads to you needing to resize it for example.

A second thing here is that your way of benchmarking these things will not be really reliable either. If you want to do actual benchmarks, check out https://benchmarkdotnet.org which will do this properly and handle things such as warmup runs, memory allocations, running on different framework versions etc.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68