Foreach, on performance. Should we declare a variable once before or inside a loop?

Which is better for performance, wise, declaring a variable outside the foreach statment and reassigning it each time (foreach) or creating a new variable inside foreach for example

private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ListItem item; foreach (var i in collection) { item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } 

or this one?

 private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (var i in collection) { ListItem item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } 

Of course I'm talking about the object of an object. thank you all.

+6
scope variable-assignment c #
source share
8 answers

It sounds like premature optimization .

First of all, do you have any reason to believe that there is a performance issue here?

Secondly, in build versions, the compiler optimizer is likely to generate identical code for both scenarios, so it probably doesn't matter. In debug builds, this may not always be true, but you don’t want optimizations there, since the intent of the debug build allows you to accurately execute the code.

+12
source share

There is a marginal case where it matters; if you "grabbed" the variable into an anonymous method / lambda. Otherwise, it is premature and does not matter. For everyone.

An example of when it matters:

 // prints all items in no particular order foreach (var i in collection) { string s = i.ToString(); ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(s); }); } 

against

 // may print the same item each time, or any combination of items; very bad string s; foreach (var i in collection) { s = i.ToString(); ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(s); }); } 
+7
source share

I am sure that the IL created by your two code blocks is identical. There should be no change in performance. However, the second block of code in which you declare the element type right where it is used is a bit more readable, and I would use it.

+4
source share

This is a very micro-optimization, and both methods are likely to be exactly the same in performance if you do not generate identical code. In this case, go for readability. I would prefer the second, since your object has no purpose outside the foreach loop.

Perhaps you can also get rid of the stored link:

 private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (var i in collection) { items.Add(new ListItem { Text = i.ToString() }); } return items; } 
+3
source share

The IL created by the two blocks should be almost the same. If you want to optimize, I would look at setting the length of the final list before filling it with elements. Thus, you will not be penalized for expanding to increase the length of the list.

Something like:

  private List<ListItem> GetItems() { var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var items = new List<ListItem>(collection.Count); //declare the amount of space here foreach (var i in collection) { ListItem item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } 
+1
source share

The same code is probably compiling, but why bother updating it. This is a good thing about the link, in this case. Once you are done with this, you can assign it to another ListItem and GC takes care of the rest.

But, on the other hand, readability for other programmers. This solution, which, of course, will not lead to a sharp change in the performance of your applications.

0
source share

Even better in your case:

 private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; foreach (var i in collection) items.Add(new ListItem { Text = i.ToString() }); return items; } 

Why create an extra variable?

0
source share

Everyone believes that IL will be identical. Also, as others have pointed out, don't worry about such things until they become a problem. Instead, ask yourself where the scope of this variable is.

The scope and context of this block of code is much more important than tiny performance optimization, which would be premature in nature and unnecessary in this scenario.

0
source share

All Articles