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.