using System.Diagnostics;
using System.Collections.Generic;
private static void Benchmark(Action act, int iterations)
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
Console.WriteLine((sw.ElapsedTicks).ToString().PadLeft(9, '0') + $"({sw.ElapsedMilliseconds})ms");
public static void Main(string[] args)
Console.WriteLine("myListToBenchmark init");
List<int> myListToBenchmark = Enumerable.Range(0, 10000)
.OrderBy(x => Guid.NewGuid())
Console.Write("foreach+add: ");
List<int> outList = new();
foreach(int v in myListToBenchmark)
Console.Write("for+add: ");
List<int> outList = new();
for(int i = 0; i < myListToBenchmark.Count(); i++)
outList.Add(myListToBenchmark[i]);
Console.Write("addRange+where: ");
List<int> outList = new();
outList.AddRange(myListToBenchmark.Where(x => true));
Console.Write("addRange+select: ");
List<int> outList = new();
outList.AddRange(myListToBenchmark.Select(i => i));
Console.Write("addRange: ");
List<int> outList = new();
outList.AddRange(myListToBenchmark);