0

Possible Duplicate:
For vs Foreach loop in C#

Lets say I have a collection

List < Foo > list = new List< Foo >();

Now which of the foolowing loops would run faster and why:

for(int i=0; i< list.Count; i++)

or

foreach(Foo foo in list)

Community
  • 1
  • 1
munna
  • 219
  • 1
  • 3
  • 13
  • 3
    possible duplicate http://stackoverflow.com/questions/1124753/for-vs-foreach-loop-in-c – hallie Dec 06 '10 at 08:28
  • As is stated in the above duplicate question: if you are having performance problems it is almost certainly **NOT** due to this – Matt Ellen Dec 06 '10 at 08:30

7 Answers7

5

It depends :

For For loop, it is on How much time does it take to evaluate the value oflist.Countor whatever value is provided in condition and How much time does it take to reference item at specific index.

For Foreach loop, it depends on How much time it takes for an iterator to return a value.

For your above example, there should not be any difference because you are using a standard List class.

decyclone
  • 28,886
  • 5
  • 58
  • 76
2

Who cares? Do you have a performance problem? If so, have you measured and determined that this is the slowest part of your app?

Jon
  • 14,742
  • 8
  • 49
  • 58
2

foreach is faster to type for me :) and easier to read.

Pabuc
  • 5,430
  • 6
  • 33
  • 51
1

Well.. you can find that out using the System.Diagnostics.StopWatch.

However the point is, why do you need to think about it. You should first consider which one is more readable and use that one instead of bothering about performance in this case.

The golden rule is always write readable code and optimize if you find a performance problem.

Unmesh Kondolikar
  • 8,964
  • 3
  • 31
  • 50
1

try this, for and foreach almost resulting the same time, but the.ForEach() method is faster

class Program
{
    static void Main(string[] args)
    {
        //Add values
        List<objClass> lst1 = new List<objClass>();
        for (int i = 0; i < 9000000; i++)
        {
            lst1.Add(new objClass("1", ""));
        }

        //For loop
        DateTime startTime = DateTime.Now;
        for (int i = 0; i < 9000000; i++)
        {
            lst1[i]._s1 = lst1[i]._s2;
        }
        Console.WriteLine((DateTime.Now - startTime).ToString());

        //ForEach Action
        startTime = DateTime.Now;
        lst1.ForEach(s => { s._s1 = s._s2; });
        Console.WriteLine((DateTime.Now - startTime).ToString());

        //foreach normal loop
        startTime = DateTime.Now;
        foreach (objClass s in lst1)
        {
            s._s1 = s._s2;
        }
        Console.WriteLine((DateTime.Now - startTime).ToString());

    }

    public class objClass
    {
        public string _s1 { get; set; }
        public string _s2 { get; set; }

        public objClass(string _s1, string _s2)
        {
            this._s1 = _s1;
            this._s2 = _s2;
        }
    }

}
Rami Alshareef
  • 6,554
  • 11
  • 40
  • 73
0

If you need to use the index of the current item, use for loop. There is no faster solution, you have proper solution only.

Cheng Chen
  • 39,413
  • 15
  • 105
  • 159
-1

I don't have a source to back this up, but I believe they will be almost if not exactly identical due to the way the compiler does optimizations such as loop unrolling. If there is a difference, it's likely on the order of single or tens of CPU cycles, which is as good as nothing for 99.9999% of applications.

In general, foreach tends to be considered 'syntactic sugar', that is, it's nice to have, but doesn't actually do much besides change the way you word a particular piece of code.

DGH
  • 9,742
  • 2
  • 20
  • 24