Performance difference between C# for-loop and Array.Fill
问题 I have implemented the following benchmark using BenchmarkDotNet : public class ForVsFillVsEnumerable { private bool[] data; [Params(10, 100, 1000)] public int N; [GlobalSetup] public void Setup() { data = new bool[N]; } [Benchmark] public void Fill() { Array.Fill(data, true); } [Benchmark] public void For() { for (int i = 0; i < data.Length; i++) { data[i] = true; } } [Benchmark] public void EnumerableRepeat() { data = Enumerable.Repeat(true, N).ToArray(); } } The results are: