105

Which code snippet will give better performance? The below code segments were written in C#.

1.

for(int tempCount=0;tempCount<list.count;tempcount++)
{
    if(list[tempCount].value==value)
    {
        // Some code.
    }
}
foreach(object row in list)
{
    if(row.value==value)
    {
        //Some coding
    }
}
Martin Brown
  • 22,802
  • 13
  • 71
  • 107
Kthevar
  • 1,547
  • 5
  • 14
  • 18
  • 31
    I imagine that it doesn't really matter. If you are having performance problems, it is almost certainly not due to this. Not that you shouldn't ask the question... – darasd Jul 14 '09 at 11:19
  • 2
    It worries me that some of the answers on here seem to be posted by people who simply do not have the concept of an iterator anywhere in their brain, and therefore no concept of enumerators or pointers. – Ed James Jul 14 '09 at 11:44
  • 3
    That 2nd code won't compile. System.Object has no member called 'value' (unless you're really wicked, have defined it as an extension method and are comparing delegates). Strongly type your foreach. – Trillian Jul 14 '09 at 11:54
  • 1
    The first code won't compile either, unless the type of `list` really does have a `count` member instead of `Count`. – Jon Skeet Jul 14 '09 at 11:59
  • Duplicates: http://stackoverflow.com/questions/44220/difference-between-foreach-and-for-loops-over-an-ienumerable-class-in-c http://stackoverflow.com/questions/472191/c-for-vs-foreach – Mark Ingram Jul 14 '09 at 12:10
  • 2
    Unless your app is very performance critical I would not worry about this. Far better to have clean and easily understandable code. – Fortyrunner Jul 14 '09 at 11:23
  • I rolled this question back one version, because removing the `if` tests stops the answers making as much sense. – Martin Brown Jan 20 '21 at 23:10

9 Answers9

130

Well, it partly depends on the exact type of list. It will also depend on the exact CLR you're using.

Whether it's in any way significant or not will depend on whether you're doing any real work in the loop. In almost all cases, the difference to performance won't be significant, but the difference to readability favours the foreach loop.

I'd personally use LINQ to avoid the "if" too:

foreach (var item in list.Where(condition))
{
}

EDIT: For those of you who are claiming that iterating over a List<T> with foreach produces the same code as the for loop, here's evidence that it doesn't:

static void IterateOverList(List<object> list)
{
    foreach (object o in list)
    {
        Console.WriteLine(o);
    }
}

Produces IL of:

.method private hidebysig static void  IterateOverList(class [mscorlib]System.Collections.Generic.List`1<object> list) cil managed
{
  // Code size       49 (0x31)
  .maxstack  1
  .locals init (object V_0,
           valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<object> V_1)
  IL_0000:  ldarg.0
  IL_0001:  callvirt   instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<object>::GetEnumerator()
  IL_0006:  stloc.1
  .try
  {
    IL_0007:  br.s       IL_0017
    IL_0009:  ldloca.s   V_1
    IL_000b:  call       instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<object>::get_Current()
    IL_0010:  stloc.0
    IL_0011:  ldloc.0
    IL_0012:  call       void [mscorlib]System.Console::WriteLine(object)
    IL_0017:  ldloca.s   V_1
    IL_0019:  call       instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<object>::MoveNext()
    IL_001e:  brtrue.s   IL_0009
    IL_0020:  leave.s    IL_0030
  }  // end .try
  finally
  {
    IL_0022:  ldloca.s   V_1
    IL_0024:  constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<object>
    IL_002a:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
    IL_002f:  endfinally
  }  // end handler
  IL_0030:  ret
} // end of method Test::IterateOverList

The compiler treats arrays differently, converting a foreach loop basically to a for loop, but not List<T>. Here's the equivalent code for an array:

static void IterateOverArray(object[] array)
{
    foreach (object o in array)
    {
        Console.WriteLine(o);
    }
}

// Compiles into...

.method private hidebysig static void  IterateOverArray(object[] 'array') cil managed
{
  // Code size       27 (0x1b)
  .maxstack  2
  .locals init (object V_0,
           object[] V_1,
           int32 V_2)
  IL_0000:  ldarg.0
  IL_0001:  stloc.1
  IL_0002:  ldc.i4.0
  IL_0003:  stloc.2
  IL_0004:  br.s       IL_0014
  IL_0006:  ldloc.1
  IL_0007:  ldloc.2
  IL_0008:  ldelem.ref
  IL_0009:  stloc.0
  IL_000a:  ldloc.0
  IL_000b:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0010:  ldloc.2
  IL_0011:  ldc.i4.1
  IL_0012:  add
  IL_0013:  stloc.2
  IL_0014:  ldloc.2
  IL_0015:  ldloc.1
  IL_0016:  ldlen
  IL_0017:  conv.i4
  IL_0018:  blt.s      IL_0006
  IL_001a:  ret
} // end of method Test::IterateOverArray

Interestingly, I can't find this documented in the C# 3 spec anywhere...

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Out of interest Jon, the scenario with List above ... does that apply to other collections also? Also, how did you know this (with no malice intended whatsoever) ... as in .. did u literally stumble across this while trying to answer this question, previously some time ago? It's so ... random / secret :) – Pure.Krome Jul 14 '09 at 11:56
  • 5
    I've been aware of the array optimisations for a while - arrays are a "core" kind of collection; the C# compiler is already deeply aware of them, so it makes sense for it to treat them differently. The compiler doesn't (and shouldn't) have any special knowledge of `List`. – Jon Skeet Jul 14 '09 at 11:59
  • Cheers :) and yeah ... array's were the first collection concept I was taught years and years ago at uni .. so it would make sence that the compiler is smart enough to deal with one of (if not the) most primitive type of collection. cheers again! – Pure.Krome Jul 14 '09 at 12:06
  • I'm definitely speculating here, but I'd imagine the reason you're not finding it in the spec is because it's a compiler optimization rather than a language specification. If this is true, I'd say that the *real* answer is implementation dependent. – senfo Jul 14 '09 at 13:12
  • @senfo: The strange thing is that the spec implies that the generated code *should* use the iterator too.Normally for something like this the spec explicitly says that it could be optimized or not. – Jon Skeet Jul 14 '09 at 13:34
  • Compare this assessment: http://stackoverflow.com/questions/225937/foreach-vs-somelist-foreach/226094#226094 – plinth Jul 14 '09 at 14:13
  • 3
    @JonSkeet Optimizing the list iterator away changes the behavior when the list is modified during iteration. You lose exception-if-modified. It is still possible to optimize, but requires checking that no modifications happen (including on other threads, I assume). – Craig Gidney Dec 06 '10 at 17:30
  • @Strilanc: Yes, it would change the behaviour. Which is probably one reason it's not being optimized away :) – Jon Skeet Dec 06 '10 at 17:38
  • @JonSkeet Use For instead of ForEach in performance-critical code paths.Says Microsoft. http://msdn.microsoft.com/en-us/library/ff647787.aspx – kbvishnu May 11 '12 at 06:51
  • 5
    @VeeKeyBee: So said Microsoft in 2004. a) things change; b) the work would have to be doing *tiny* amounts of work on each iteration for this to be significant. Note that `foreach` over an array is equivalent to `for` anyway. *Always* code for readability first, then only micro-optimize when you have *evidence* that it gives a measurable performance benefit. – Jon Skeet May 11 '12 at 07:08
14

A for loop gets compiled to code approximately equivalent to this:

int tempCount = 0;
while (tempCount < list.Count)
{
    if (list[tempCount].value == value)
    {
        // Do something
    }
    tempCount++;
}

Where as a foreach loop gets compiled to code approximately equivalent to this:

using (IEnumerator<T> e = list.GetEnumerator())
{
    while (e.MoveNext())
    {
        T o = (MyClass)e.Current;
        if (row.value == value)
        {
            // Do something
        }
    }
}

So as you can see, it would all depend upon how the enumerator is implemented versus how the lists indexer is implemented. As it turns out the enumerator for types based on arrays are normally written something like this:

private static IEnumerable<T> MyEnum(List<T> list)
{
    for (int i = 0; i < list.Count; i++)
    {
        yield return list[i];
    }
}

So as you can see, in this instance it won't make very much difference, however the enumerator for a linked list would probably look something like this:

private static IEnumerable<T> MyEnum(LinkedList<T> list)
{
    LinkedListNode<T> current = list.First;
    do
    {
        yield return current.Value;
        current = current.Next;
    }
    while (current != null);
}

In .NET you will find that the LinkedList<T> class does not even have an indexer, so you wouldn't be able to do your for loop on a linked list; but if you could, the indexer would have to be written like so:

public T this[int index]
{
       LinkedListNode<T> current = this.First;
       for (int i = 1; i <= index; i++)
       {
            current = current.Next;
       }
       return current.value;
}

As you can see, calling this multiple times in a loop is going to be much slower than using an enumerator that can remember where it is in the list.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Martin Brown
  • 22,802
  • 13
  • 71
  • 107
  • Calling this multiple times in a for loop would give poor performance, but a badly designed indexing function is not an argument against using for, just an argument against using badly designed functions. for loops don't require an indexer and can take a LinkedListNode instead of the int perfectly well, negating the need for this 'search for index loop'. Likely the c# devs didn't include an indexer for LinkedList in order to stop people directly porting across code from List and arrays without realising it would be is O(N) lookup rather than the O(1) from other types. – Paul Childs Jan 17 '21 at 23:11
12

An easy test to semi-validate. I did a small test, just to see. Here is the code:

static void Main(string[] args)
{
    List<int> intList = new List<int>();

    for (int i = 0; i < 10000000; i++)
    {
        intList.Add(i);
    }

    DateTime timeStarted = DateTime.Now;
    for (int i = 0; i < intList.Count; i++)
    {
        int foo = intList[i] * 2;
        if (foo % 2 == 0)
        {
        }
    }

    TimeSpan finished = DateTime.Now - timeStarted;

    Console.WriteLine(finished.TotalMilliseconds.ToString());
    Console.Read();

}

And here is the foreach section:

foreach (int i in intList)
{
    int foo = i * 2;
    if (foo % 2 == 0)
    {
    }
}

When I replaced the for with a foreach -- the foreach was 20 milliseconds faster -- consistently. The for was 135-139ms while the foreach was 113-119ms. I swapped back and forth several times, making sure it wasn't some process that just kicked in.

However, when I removed the foo and the if statement, the for was faster by 30 ms (foreach was 88ms and for was 59ms). They were both empty shells. I'm assuming the foreach actually passed a variable where as the for was just incrementing a variable. If I added

int foo = intList[i];

Then the for become slow by about 30ms. I'm assuming this had to do with it creating foo and grabbing the variable in the array and assigning it to foo. If you just access intList[i] then you don't have that penalty.

In all honesty.. I expected the foreach to be slightly slower in all circumstances, but not enough to matter in most applications.

edit: here is the new code using Jons suggestions (134217728 is the biggest int you can have before System.OutOfMemory exception gets thrown):

static void Main(string[] args)
{
    List<int> intList = new List<int>();

    Console.WriteLine("Generating data.");
    for (int i = 0; i < 134217728 ; i++)
    {
        intList.Add(i);
    }

    Console.Write("Calculating for loop:\t\t");

    Stopwatch time = new Stopwatch();
    time.Start();
    for (int i = 0; i < intList.Count; i++)
    {
        int foo = intList[i] * 2;
        if (foo % 2 == 0)
        {
        }
    }

    time.Stop();
    Console.WriteLine(time.ElapsedMilliseconds.ToString() + "ms");
    Console.Write("Calculating foreach loop:\t");
    time.Reset();
    time.Start();

    foreach (int i in intList)
    {
        int foo = i * 2;
        if (foo % 2 == 0)
        {
        }
    }

    time.Stop();

    Console.WriteLine(time.ElapsedMilliseconds.ToString() + "ms");
    Console.Read();
}

And here are the results:

Generating data. Calculating for loop: 2458ms Calculating foreach loop: 2005ms

Swapping them around to see if it deals with the order of things yields the same results (nearly).

akjoshi
  • 14,589
  • 13
  • 94
  • 116
Kenny Mann
  • 871
  • 10
  • 24
  • 6
    It's better to use Stopwatch than DateTime.Now - and I wouldn't trust any run that fast, to be honest. – Jon Skeet Jul 14 '09 at 13:36
  • 9
    Your foreach loops are running faster because a 'for' evaluates the condition each iteration. In the case of your example, this makes for one extra method call (to get list.count) In short, you are benchmarking two different pieces of code, hence your strange results. Try 'int max = intlist.Count; for(int i = 0; i – A.R. Feb 27 '12 at 19:42
  • 1
    After compilation, for and foreach optimize to exactly the same thing when working with primitives. It isn't until you introduce List That they differ (greatly) in speed. – Anthony Russell Jun 16 '15 at 16:47
9

Note: this answer applies more to Java than it does to C#, since C# doesn't have an indexer on LinkedLists, but I think the general point still holds.

If the list you're working with happens to be a LinkedList, the performance of the indexer-code (array-style accessing) is a lot worse than using the IEnumerator from the foreach, for large lists.

When you access element 10.000 in a LinkedList using the indexer syntax: list[10000], the linked list will start at the head node, and traverse the Next-pointer ten thousand times, until it reaches the correct object. Obviously, if you do this in a loop, you will get:

list[0]; // head
list[1]; // head.Next
list[2]; // head.Next.Next
// etc.

When you call GetEnumerator (implicitly using the forach-syntax), you'll get an IEnumerator object that has a pointer to the head node. Each time you call MoveNext, that pointer is moved to the next node, like so:

IEnumerator em = list.GetEnumerator();  // Current points at head
em.MoveNext(); // Update Current to .Next
em.MoveNext(); // Update Current to .Next
em.MoveNext(); // Update Current to .Next
// etc.

As you can see, in the case of LinkedLists, the array indexer method becomes slower and slower, the longer you loop (it has to go through the same head pointer over and over again). Whereas the IEnumerable just operates in constant time.

Of course, as Jon said this really depends on the type of list, if the list is not a LinkedList, but an array, the behavior is completely different.

Tom Lokhorst
  • 12,397
  • 5
  • 50
  • 69
  • 4
    LinkedList in .NET doesn't have an indexer, so it's not actually an option. – Jon Skeet Jul 14 '09 at 13:35
  • Oh, well that solves that problem, then :-) I'm just looking through the `LinkedList` docs on MSDN, and it has a pretty decent API. Most importantly, it doesn't have a `get(int index)` method, like Java does. Still, I guess the point still holds for any other list-like data structure that exposes an indexer that's slower than a specific `IEnumerator`. – Tom Lokhorst Jul 14 '09 at 13:53
2

Like other people have mentioned though the performance doesn't actually matter much, the foreach will always be a little bit slower because of the IEnumerable/IEnumerator usage in the loop. The compiler translates the construct into calls on that interface and for every step a function + a property are called in the foreach construct.

IEnumerator iterator = ((IEnumerable)list).GetEnumerator();
while (iterator.MoveNext()) {
  var item = iterator.Current;
  // do stuff
}

This is the equivalent expansion of the construct in C#. You can imagine how the performance impact can vary based on the implementations of MoveNext and Current. Whereas in an array access, you don't have that dependencies.

Jason Plank
  • 2,322
  • 4
  • 29
  • 39
Charles Prakash Dasari
  • 4,539
  • 1
  • 23
  • 44
  • 4
    Don't forget there's a difference between an array access and an indexer access. If list is a `List` here then there's still the hit (possibly inlined) of calling the indexer. It's not like it's a bare metal array access. – Jon Skeet Jul 14 '09 at 11:47
  • Very true! It is yet another property execution and we are at the mercy of the implementation. – Charles Prakash Dasari Jul 14 '09 at 16:59
2

After reading enough arguments that "the foreach loop should be preferred for readability", I can say that my first reaction was "what"? Readability, in general, is subjective and, in this particular instance, even more. For someone with a background in programming(practically, every language before Java), for loops are much easier to read than foreach loops. In addition, the same people claiming that foreach loops are more readable, are also supporters of linq and other "features" that make code hard to read and maintain, something that proves the above point.

About the impact on performance, see the answer to this question.

EDIT: There are collections in C#(like the HashSet) that have no indexer. In these collections, foreach is the only way to iterate and it is the only case I think it should be used over for.

Community
  • 1
  • 1
ThunderGr
  • 2,071
  • 23
  • 20
0

There is a further interesting fact which can be easy missed when testing the speed of both loops: Using the debug mode doesn't let the compiler optimize the code using the default settings.

This led me to the interesting result that the foreach is faster than for in the debug mode. Whereas the for ist faster than foreach in the release mode. Obviously the compiler has better ways to optimize a for loop than a foreach loop which compromises several method calls. A for loop is by the way such fundamental that it's possible that this is even optimized by the CPU itself.

Sam
  • 1,167
  • 1
  • 10
  • 25
0

In the example you provided, it is definitely better to use foreach loop instead a for loop.

The standard foreach construct can be faster (1,5 cycles per step) than a simple for-loop (2 cycles per step), unless the loop has been unrolled (1.0 cycles per step).

So for everyday code, performance is not a reason to use the more complex for, while or do-while constructs.

Check out this link: http://www.codeproject.com/Articles/146797/Fast-and-Less-Fast-Loops-in-C


╔══════════════════════╦═══════════╦═══════╦════════════════════════╦═════════════════════╗
║        Method        ║ List<int> ║ int[] ║ Ilist<int> onList<Int> ║ Ilist<int> on int[] ║
╠══════════════════════╬═══════════╬═══════╬════════════════════════╬═════════════════════╣
║ Time (ms)            ║ 23,80     ║ 17,56 ║ 92,33                  ║ 86,90               ║
║ Transfer rate (GB/s) ║ 2,82      ║ 3,82  ║ 0,73                   ║ 0,77                ║
║ % Max                ║ 25,2%     ║ 34,1% ║ 6,5%                   ║ 6,9%                ║
║ Cycles / read        ║ 3,97      ║ 2,93  ║ 15,41                  ║ 14,50               ║
║ Reads / iteration    ║ 16        ║ 16    ║ 16                     ║ 16                  ║
║ Cycles / iteration   ║ 63,5      ║ 46,9  ║ 246,5                  ║ 232,0               ║
╚══════════════════════╩═══════════╩═══════╩════════════════════════╩═════════════════════╝

rpax
  • 4,306
  • 6
  • 29
  • 53
  • 4
    You might re-read the code project article you linked. It is a interesting article, but it says the exact opposite of your post. Also, the table you recreated is measuring the performance of accessing an array and List directly, or via their IList interfaces. Neither have anything to do with the question. :) – Paul Walls Feb 25 '14 at 04:53
0

you can read about it in Deep .NET - part 1 Iteration

it's cover the results (without the first initialization) from .NET source code all the way to the disassembly.

for example - Array Iteration with a foreach loop: enter image description here

and - list iteration with foreach loop: enter image description here

and the end results: enter image description here

enter image description here

Or Yaacov
  • 2,880
  • 3
  • 15
  • 39