In .NET, which loop runs faster, 'for' or 'foreach'?

前端 未结 30 1824
抹茶落季
抹茶落季 2020-11-22 04:25

In C#/VB.NET/.NET, which loop runs faster, for or foreach?

Ever since I read that a for loop works faster than a foreach

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 04:54

    I found the foreach loop which iterating through a List faster. See my test results below. In the code below I iterate an array of size 100, 10000 and 100000 separately using for and foreach loop to measure the time.

    enter image description here

    private static void MeasureTime()
        {
            var array = new int[10000];
            var list = array.ToList();
            Console.WriteLine("Array size: {0}", array.Length);
    
            Console.WriteLine("Array For loop ......");
            var stopWatch = Stopwatch.StartNew();
            for (int i = 0; i < array.Length; i++)
            {
                Thread.Sleep(1);
            }
            stopWatch.Stop();
            Console.WriteLine("Time take to run the for loop is {0} millisecond", stopWatch.ElapsedMilliseconds);
    
            Console.WriteLine(" ");
            Console.WriteLine("Array Foreach loop ......");
            var stopWatch1 = Stopwatch.StartNew();
            foreach (var item in array)
            {
                Thread.Sleep(1);
            }
            stopWatch1.Stop();
            Console.WriteLine("Time take to run the foreach loop is {0} millisecond", stopWatch1.ElapsedMilliseconds);
    
            Console.WriteLine(" ");
            Console.WriteLine("List For loop ......");
            var stopWatch2 = Stopwatch.StartNew();
            for (int i = 0; i < list.Count; i++)
            {
                Thread.Sleep(1);
            }
            stopWatch2.Stop();
            Console.WriteLine("Time take to run the for loop is {0} millisecond", stopWatch2.ElapsedMilliseconds);
    
            Console.WriteLine(" ");
            Console.WriteLine("List Foreach loop ......");
            var stopWatch3 = Stopwatch.StartNew();
            foreach (var item in list)
            {
                Thread.Sleep(1);
            }
            stopWatch3.Stop();
            Console.WriteLine("Time take to run the foreach loop is {0} millisecond", stopWatch3.ElapsedMilliseconds);
        }
    

    UPDATED

    After @jgauffin suggestion I used @johnskeet code and found that the for loop with array is faster than following,

    • Foreach loop with array.
    • For loop with list.
    • Foreach loop with list.

    See my test results and code below,

    enter image description here

    private static void MeasureNewTime()
        {
            var data = new double[Size];
            var rng = new Random();
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = rng.NextDouble();
            }
            Console.WriteLine("Lenght of array: {0}", data.Length);
            Console.WriteLine("No. of iteration: {0}", Iterations);
            Console.WriteLine(" ");
            double correctSum = data.Sum();
    
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                double sum = 0;
                for (int j = 0; j < data.Length; j++)
                {
                    sum += data[j];
                }
                if (Math.Abs(sum - correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("For loop with Array: {0}", sw.ElapsedMilliseconds);
    
            sw = Stopwatch.StartNew();
            for (var i = 0; i < Iterations; i++)
            {
                double sum = 0;
                foreach (double d in data)
                {
                    sum += d;
                }
                if (Math.Abs(sum - correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("Foreach loop with Array: {0}", sw.ElapsedMilliseconds);
            Console.WriteLine(" ");
    
            var dataList = data.ToList();
            sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                double sum = 0;
                for (int j = 0; j < dataList.Count; j++)
                {
                    sum += data[j];
                }
                if (Math.Abs(sum - correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("For loop with List: {0}", sw.ElapsedMilliseconds);
    
            sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                double sum = 0;
                foreach (double d in dataList)
                {
                    sum += d;
                }
                if (Math.Abs(sum - correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("Foreach loop with List: {0}", sw.ElapsedMilliseconds);
        }
    

提交回复
热议问题